Code: Select all
local player = minetest.get_player_by_name(name)
"currentplayer", "username", current_player, name and get_player_name()
The message shown is "attempt to call global 'get_player_by_name' (a nil value)
Code: Select all
local player = minetest.get_player_by_name(name)
See the developer-wiki (caution, infos may be outdated).Chris wrote:I try to get the current player:Code: Select all
local player = minetest.get_player_by_name(name)
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})
If the above snippet is your whole script, it would run the moment when MT starts,Chris wrote:I've tried the example from wiki:debug.txt says: ..: attempt to index local 'player' (a nil value)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})
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 wrote:I try to get the current player:but I don't know the name of the current_player. I've tried alreadyCode: Select all
local player = minetest.get_player_by_name(name)
"currentplayer", "username", current_player, name and get_player_name()
The message shown is "attempt to call global 'get_player_by_name' (a nil value)
Is there a workaround to initialize the player before the snipped is executed?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.
You could try to delay it:Chris wrote:Is there a workaround to initialize the player before the snipped is executed?
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)
Code: Select all
minetest.register_on_joinplayer(function(player)
if player then
...
end
end)
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>
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)
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>
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,
})
Users browsing this forum: No registered users and 1 guest