How to use "get_player_by_name()"?

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

How to use "get_player_by_name()"?

by Chris » Post

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: 602
Joined: Thu Oct 13, 2016 10:45
Location: DE
Contact:

Re: How to use "get_player_by_name()"?

by hajo » Post

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: 4526
Joined: Thu Oct 03, 2013 07:48
GitHub: SmallJoker
Location: Switzerland
Contact:

Re: How to use "get_player_by_name()"?

by Krock » Post

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
Look, I programmed a bug for you. >> 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 » Post

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: 602
Joined: Thu Oct 13, 2016 10:45
Location: DE
Contact:

Re: How to use "get_player_by_name()"?

by hajo » Post

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: 2386
Joined: Wed Oct 19, 2016 15:18
GitHub: Andrey2470T
In-game: Andrey01
Location: Russia, Moscow

Re: How to use "get_player_by_name()"?

by Andrey01 » Post

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.

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

Re: How to use "get_player_by_name()"?

by Chris » Post

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: 4526
Joined: Thu Oct 03, 2013 07:48
GitHub: SmallJoker
Location: Switzerland
Contact:

Re: How to use "get_player_by_name()"?

by Krock » Post

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)
Look, I programmed a bug for you. >> Mod Search Engine << - Mods by Krock - DuckDuckGo mod search bang: !mtmod <keyword here>

Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests