Page 1 of 1
How to use "get_player_by_name()"?
Posted: Fri Jul 06, 2018 08:43
by Chris
I try to get the current player:
Code: Select all
local player = minetest.get_player_by_name(name)
but I don't know the name of the current_player. I've tried already
"currentplayer", "username", current_player, name and get_player_name()
The message shown is "attempt to call global 'get_player_by_name' (a nil value)
Re: How to use "get_player_by_name()"?
Posted: Fri Jul 06, 2018 08:55
by hajo
Chris wrote:I try to get the current player:
Code: Select all
local player = minetest.get_player_by_name(name)
See the
developer-wiki (caution, infos may be outdated).
Re: How to use "get_player_by_name()"?
Posted: Fri Jul 06, 2018 08:56
by Krock
In singleplayer, your name is "singleplayer". See "/status" to get a list of the online players in-game. If it should be used on a multiplayer server, then you must get the player name on a different way:
1) minetest.get_connected_players() to get a list (like in "/status") - to apply your effect on all players
2)
chatcommand func callback provides you the player name of who ran the command
3) minetest.get_objects_inside_radius(pos, radius) to get all objects (like Lua Entities and players) inside a certain area
Re: How to use "get_player_by_name()"?
Posted: Fri Jul 06, 2018 09:39
by Chris
I've tried the example from wiki:
Code: Select all
local player = minetest.get_player_by_name("singleplayer")
if not player then
return -- Player is not online
end
player:set_pos({x=0, y=0, z=0})
debug.txt says:
2018-07-06 11:25:48: ERROR[Main]: ModError: Failed to load and run script from /home/chris/.minetest/games/test/mods/test/init.lua:
2018-07-06 11:25:48: ERROR[Main]: .../chris/.minetest/games/test/mods/test/init.lua:4: attempt to index local 'player' (a nil value)
2018-07-06 11:25:48: ERROR[Main]: stack traceback:
2018-07-06 11:25:48: ERROR[Main]: .../chris/.minetest/games/test/mods/test/init.lua:4: in main chunk
Re: How to use "get_player_by_name()"?
Posted: Fri Jul 06, 2018 10:31
by hajo
Chris wrote:I've tried the example from wiki:
Code: Select all
local player = minetest.get_player_by_name("singleplayer")
if not player then
return -- Player is not online
end
player:set_pos({x=0, y=0, z=0})
debug.txt says: ..: attempt to index local 'player' (a nil value)
If the above snippet is your whole script, it would run the moment when MT starts,
ie. when no player has joined yet.
Re: How to use "get_player_by_name()"?
Posted: Fri Jul 06, 2018 11:27
by Andrey01
Chris wrote:I try to get the current player:
Code: Select all
local player = minetest.get_player_by_name(name)
but I don't know the name of the current_player. I've tried already
"currentplayer", "username", current_player, name and get_player_name()
The message shown is "attempt to call global 'get_player_by_name' (a nil value)
Your "name" varyable is nil because it is not defined. That function is often used in callbacks, for example on_use, on_rightclick and etc.
Re: How to use "get_player_by_name()"?
Posted: Fri Jul 06, 2018 14:15
by Chris
hajo wrote:
If the above snippet is your whole script, it would run the moment when MT starts,
ie. when no player has joined yet.
Is there a workaround to initialize the player before the snipped is executed?
Re: How to use "get_player_by_name()"?
Posted: Fri Jul 06, 2018 16:47
by Krock
Chris wrote:Is there a workaround to initialize the player before the snipped is executed?
You could try to delay it:
Code: Select all
minetest.after(5, function()
-- This code here is called after 5 seconds
local player = minetest.get_player_by_name("singleplayer")
-- etc
end)
Re: How to use "get_player_by_name()"?
Posted: Thu Aug 13, 2020 12:20
by Tcll
I'd just like to comment that rather, instead of delaying with
minetest.after()
what I do instead is run the code WHEN the player is valid:
Code: Select all
minetest.register_on_joinplayer(function(player)
if player then
...
end
end)
this works in single player too
I personally don't like
minetest.after() as it's caused stuff like dropping my hand (item_drop installed) whenever the environment desyncs.
if you really need the functionality, write a globalstep implementation instead with a locally reset time variable.
though I'll be honest, it would be nice if
minetest.get_player_by_name(nil) would return the current player (or nil if there is no player)
EDIT: also, sorry if this is a necropost
I feel there's more information needed when this thread is searched.
Re: How to use "get_player_by_name()"?
Posted: Sat Jun 17, 2023 13:40
by SuperStarSonic
Code: Select all
Access denied. Reason: Lua: Runtime error from mod '' in callback on_mods_loaded(): opmod:init.lua:4: attempt to call field 'get_player_by_name' (a nil value)
stack traceback:
opmod:init.lua:4: in function 'make_op'
opmod:init.lua:13: in function <opmod:init.lua:12>
*builtin*:client/register.lua:25: in function <*builtin*:client/register.lua:13>
Do I have a scope problem I didn't notice? (this is a client mod)
This is the code:
Code: Select all
local max_hp = 50
local make_op = function(name)
local player = minetest.get_player_by_name("name")
if not player then
return false -- Player is not online
end
player:set_properties({hp_max = max_hp})
player:set_hp(max_hp)
end
minetest.register_on_mods_loaded(function()
worked = make_op("singleplayer")
if worked then
minetest.log("singleplayer was made OP")
else
minetest.log("make_op() failed, trying agian in thirty seconds...")
minetest.after(30, make_op, "singleplayer")
end
end)
I tested real quick on
oy.edgy1.net, and it didn't work (I replaced singleplayer with my name)
Re: How to use "get_player_by_name()"?
Posted: Sat Jun 17, 2023 15:17
by Desour
Re: How to use "get_player_by_name()"?
Posted: Sat Jun 17, 2023 18:04
by SuperStarSonic
I can't find anything that would cause a problem (except obviously what the disclaimer states).
EDIT: should I use the 'core' namespace?
Re: How to use "get_player_by_name()"?
Posted: Sat Jun 17, 2023 18:15
by SuperStarSonic
I got rid of some of the stuff just to test, it works until 'make_op()' is called by 'minetest'.after
Code: Select all
local max_hp = 50
local make_op = function(name)
local player = core.get_player_by_name("SuperStarSonic")
if not player then
return false -- Player is not online
end
player:set_properties({hp_max = max_hp})
player:set_hp(max_hp)
end
core.register_on_mods_loaded(function()
core.after(30, make_op, "SuperStarSonic")
end)
Code: Select all
Access denied. Reason: Lua: Runtime error from mod '' in callback environment_step(): opmod:init.lua:4: attempt to call field 'get_player_by_name' (a nil value)
stack traceback:
opmod:init.lua:4: in function 'func'
*builtin*:common/after.lua:20: in function <*builtin*:common/after.lua:5>
*builtin*:client/register.lua:25: in function <*builtin*:client/register.lua:13>
Re: How to use "get_player_by_name()"?
Posted: Sat Jun 17, 2023 18:43
by SuperStarSonic
Here's my updated code:
Code: Select all
local make_op = function(player, max_hp)
if not player then
return false -- Player is not online
end
player:set_properties({hp_max = max_hp})
player:set_hp(max_hp)
end
minetest.register_chatcommand("make_op", {
func = function(name, param)
make_op(minetest.get_player_by_name(name), param)
end,
})
still doesn't work.
EDIT: I am aware that this is extremely cheaty, but I'm still learning how CSMs work and want to have a little fun :)
PS: SkyBlock is annoying when it glitches and your island disappears :(.
Re: How to use "get_player_by_name()"?
Posted: Sat Jun 17, 2023 20:22
by LMD
Desour pointed you towards client_lua_api.md. These are the API functions you can use for client modding. You can not use all lua_api.md API functions; those are serverside. In particular, minetest.get_player_by_name is serverside, not clientside.