Page 1 of 1

How to use "get_player_by_name()"?

PostPosted: 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()"?

PostPosted: 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()"?

PostPosted: 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()"?

PostPosted: 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()"?

PostPosted: 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()"?

PostPosted: 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()"?

PostPosted: 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()"?

PostPosted: 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)