How to use "get_player_by_name()"?

Chris
New member
 
Posts: 5
Joined: Thu Mar 01, 2018 11:33

How to use "get_player_by_name()"?

by Chris » Fri Jul 06, 2018 08:43

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)
 

hajo
Member
 
Posts: 594
Joined: Thu Oct 13, 2016 10:45
Location: DE

Re: How to use "get_player_by_name()"?

by hajo » Fri Jul 06, 2018 08:55

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).
 

User avatar
Krock
Developer
 
Posts: 4106
Joined: Thu Oct 03, 2013 07:48
Location: Switzerland
GitHub: SmallJoker

Re: How to use "get_player_by_name()"?

by Krock » Fri Jul 06, 2018 08:56

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
Mod Search Engine - Mods by Krock - DuckDuckGo mod search bang: !mtmod <keyword here>
 

Chris
New member
 
Posts: 5
Joined: Thu Mar 01, 2018 11:33

Re: How to use "get_player_by_name()"?

by Chris » Fri Jul 06, 2018 09:39

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
 

hajo
Member
 
Posts: 594
Joined: Thu Oct 13, 2016 10:45
Location: DE

Re: How to use "get_player_by_name()"?

by hajo » Fri Jul 06, 2018 10:31

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.
 

User avatar
Andrey01
Member
 
Posts: 2130
Joined: Wed Oct 19, 2016 15:18
Location: Russia, Moscow
GitHub: Andrey2470T
In-game: Andrey01

Re: How to use "get_player_by_name()"?

by Andrey01 » Fri Jul 06, 2018 11:27

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.
DL9Eh7xVBw7DXwLAMX1lStE21bSD2XUQ
 

Chris
New member
 
Posts: 5
Joined: Thu Mar 01, 2018 11:33

Re: How to use "get_player_by_name()"?

by Chris » Fri Jul 06, 2018 14:15

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?
 

User avatar
Krock
Developer
 
Posts: 4106
Joined: Thu Oct 03, 2013 07:48
Location: Switzerland
GitHub: SmallJoker

Re: How to use "get_player_by_name()"?

by Krock » Fri Jul 06, 2018 16:47

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)
Mod Search Engine - Mods by Krock - DuckDuckGo mod search bang: !mtmod <keyword here>
 


Return to Modding Discussion



Who is online

Users browsing this forum: Google [Bot] and 4 guests