Page 1 of 1

[DONE]Need help, dont find the Problem

Posted: Sat Nov 12, 2016 11:40 am
by LuziferSenpai
Hey,

in this Script is a fail and I dont find it!
Please help me!

Code: Select all

script.on_event( defines.events.on_player_created, function( event )
	create_gui( game.players[event.player_index] )
end )

Code: Select all

local function create_gui( player )
	local top = player.gui.top
	if top["supply-toggle-button"] then
		if player.force.technologies["rocket-silo"].researched ~= true then
			top["supply-toggle-button"].destroy()
		end
		return
	end

	if player.force.technologies["rocket-silo"].researched then
		top.add{ type = "button", name = "supply-toggle-button", caption = { "supply-toggle-button-caption" } }
	end
end
The Error i get:

Code: Select all

Error while running event on_player_created (ID 24)
__Xter_Server_MOD__/control.lua:45: attempt to call global 'create_gui' (a nil value)
Greetz,

Luzifer

Re: Need help, dont find the Problem

Posted: Sat Nov 12, 2016 11:59 am
by steinio
As the error message states it tries to call a global function which you have declared as local.

Remove the local before the declaration of your function.

Greetings steinio

Re: Need help, dont find the Problem

Posted: Sat Nov 12, 2016 12:46 pm
by orzelek
Make sure that create_gui definition is above the call of it in the file.
If event definition code is before it then function is not known to interpreter yet and gives an error.

Re: Need help, dont find the Problem

Posted: Sat Nov 12, 2016 12:59 pm
by LuziferSenpai
steinio wrote:As the error message states it tries to call a global function which you have declared as local.

Remove the local before the declaration of your function.

Greetings steinio
But in this Code its working:

Code: Select all

script.on_event(defines.events.on_player_created, function(event)
	create_change_player_color_button_for_player(game.players[event.player_index])
end)

Code: Select all

local function create_change_player_color_button_for_player(player)
	local top = player.gui.top
	if not top[player_color_defines.names.gui.change_color_button] then
		top.add{type = "button", name = player_color_defines.names.gui.change_color_button, style = player_color_defines.names.gui_style.change_color_button, tooltip = {"gui.change-player-color"}}
	end
end

Re: Need help, dont find the Problem

Posted: Sat Nov 12, 2016 1:00 pm
by LuziferSenpai
orzelek wrote:Make sure that create_gui definition is above the call of it in the file.
If event definition code is before it then function is not known to interpreter yet and gives an error.
Okay thanks