[Solved] Attempt to index global 'script' (a nil value)

Place to get help with not working mods / modding interface.
Post Reply
BrokenScience
Inserter
Inserter
Posts: 26
Joined: Thu Jul 21, 2016 5:31 pm
Contact:

[Solved] Attempt to index global 'script' (a nil value)

Post by BrokenScience »

The game does not allow me to use script.on_event or any other of the functions stored in script. It works fine with all my other mods but, in this particular one, crashes and brings up the error: "attempt to index global 'script' (a nil value)". What is causing this and how do I fix it?
Last edited by BrokenScience on Tue Aug 01, 2017 1:46 am, edited 2 times in total.
Smashing a brick wall with my face would be a lot more rewarding if I didn't just reveal 3 more.

Nexela
Smart Inserter
Smart Inserter
Posts: 1816
Joined: Wed May 25, 2016 11:09 am
Contact:

Re: Attempt to index global 'script' (a nil value)

Post by Nexela »

script is only valid from control.lua (and files you require from control.lua)

User avatar
darkfrei
Smart Inserter
Smart Inserter
Posts: 2366
Joined: Thu Nov 20, 2014 11:11 pm
Contact:

Re: Attempt to index global 'script' (a nil value)

Post by darkfrei »

Can you show your code?

User avatar
bobingabout
Smart Inserter
Smart Inserter
Posts: 6890
Joined: Fri May 09, 2014 1:01 pm
Contact:

Re: Attempt to index global 'script' (a nil value)

Post by bobingabout »

I can't help you figure out what is wrong, since you haven't shown us anything.
Literally all you've told us is "A script in my mod doesn't work", okay, that's nice, more info please?
Creator of Bob's mods. Expanding your gameplay since version 0.9.8.
I also have a Patreon.

BrokenScience
Inserter
Inserter
Posts: 26
Joined: Thu Jul 21, 2016 5:31 pm
Contact:

Re: Attempt to index global 'script' (a nil value)

Post by BrokenScience »

Code: Select all

script.on_event("Eco", function(event)
	for each in data do
		if each[type] == "recipe" then
			table.insert(recipes, each)
		end
	end
	local test = player.gui.center.add({type = "frame", name = "test", direction = "vertical"})
	local frame = ui.add({type = "frame", name = "test_frame", direction = "vertical"})
	for each in recipes do
		local label = frame.add({type = "label", name = each[name], caption = each[name]})
	end
end)
This is the code. Ignore the contents of the function, I know it is wrong. I am trying to test something.

"Eco" is a custom key. All of this is in control.lua for the mod. If I put this script.on_event into on_mod_load or on_mod_init function it no longer has the error but is completely useless.
Smashing a brick wall with my face would be a lot more rewarding if I didn't just reveal 3 more.

Nexela
Smart Inserter
Smart Inserter
Posts: 1816
Joined: Wed May 25, 2016 11:09 am
Contact:

Re: Attempt to index global 'script' (a nil value)

Post by Nexela »

data is only availble in the data stage, script is only available in the control stage, you are trying to mix the 2

http://lua-api.factorio.com/latest/Data-Lifecycle.html

BrokenScience
Inserter
Inserter
Posts: 26
Joined: Thu Jul 21, 2016 5:31 pm
Contact:

Re: Attempt to index global 'script' (a nil value)

Post by BrokenScience »

That was it. How do I access the recipes and items then in this stage if I cannot use data?
Smashing a brick wall with my face would be a lot more rewarding if I didn't just reveal 3 more.

User avatar
prg
Filter Inserter
Filter Inserter
Posts: 947
Joined: Mon Jan 19, 2015 12:39 am
Contact:

Re: Attempt to index global 'script' (a nil value)

Post by prg »

Automatic Belt (and pipe) Planner—Automate yet another aspect of constructing your factory!

BrokenScience
Inserter
Inserter
Posts: 26
Joined: Thu Jul 21, 2016 5:31 pm
Contact:

Re: Attempt to index global 'script' (a nil value)

Post by BrokenScience »

I replaced data now with game.recipe_prototypes so now I have this:

Code: Select all

script.on_event("Eco", function(event)
	local test = player.gui.center.add({type = "frame", name = "test", direction = "vertical"})
	local frame = ui.add({type = "frame", name = "test_frame", direction = "vertical"})
	for each in game.recipe_prototypes do
		local label = frame.add({type = "label", name = each[name], caption = each[name]})
	end
end)
I now have the same error again (Attempt to index global 'script' (a nil value)). What is wrong with this to cause script.on_event to not register?
Smashing a brick wall with my face would be a lot more rewarding if I didn't just reveal 3 more.

User avatar
Mooncat
Smart Inserter
Smart Inserter
Posts: 1174
Joined: Wed May 18, 2016 4:55 pm
Contact:

Re: Attempt to index global 'script' (a nil value)

Post by Mooncat »

It will be more helpful if you can provide us the whole lua file instead of just the snippet.

The error message is a bit strange. If the code is really in control.lua, "script" shouldn't be nil. But the error says it is...

Right now, I can only tell that you are doing wrong with the event system. If you want to use custom event, you will need to register it first using script.generate_event_name() and use the returned value to replace "Eco".
Also, when you raise the event, you should use the same returned value of script.generate_event_name(). Because if you read the document carefully, script.raise_event actually accepts the unsigned-integral ID of of the event, not the event name (string).
My mods (Deprecated, maybe temporary... idk. Currently time/mood to continue):

BrokenScience
Inserter
Inserter
Posts: 26
Joined: Thu Jul 21, 2016 5:31 pm
Contact:

Re: Attempt to index global 'script' (a nil value)

Post by BrokenScience »

Mooncat wrote:It will be more helpful if you can provide us the whole lua file instead of just the snippet.
The code above is the entire Lua file of control.Lua with the exception of a single comment above it that says, "-- test", but I added it anyway.

What is the difference between:

Code: Select all

data:extend({
	{
    type = "custom-input",
    name = "Eco",
    key_sequence = "SHIFT + R",
    consuming = "script-only"
	}
})
In data.Lua and what I have above in control.lua and what you suggested? I have another mod that uses the same method for events and works fine (ItemGiverGui). I have been using this as an example for Gui and events but i'm getting the feeling that it may not be the best example. (I attached the control.lua of the mod I have as I added comments that made it easier for me to scan and might help.)

Also I am confused as to where the linking of the custom input and code to the event happens with your method. Is this in addition to what I have right now? Does the custom input and code get passed to script.raise_event(event, table) in the table somewhere?
Attachments
control.lua
ItemGiverGui control.lua with comments I added
(13.63 KiB) Downloaded 57 times
control.lua
My control.lua
(367 Bytes) Downloaded 66 times
Smashing a brick wall with my face would be a lot more rewarding if I didn't just reveal 3 more.

Nexela
Smart Inserter
Smart Inserter
Posts: 1816
Joined: Wed May 25, 2016 11:09 am
Contact:

Re: Attempt to index global 'script' (a nil value)

Post by Nexela »

data is where prototypes go, control is where game scripts go

control.lua

Code: Select all

script.on_event("Eco", function(event)
    local player = game.players[event.player_index]
	local main_frame = player.gui.center.add({type = "frame", name = "test", direction = "vertical"})
	local frame = main_frame.add({type = "frame", name = "test_frame", direction = "vertical"})
	for recipe_name in pairs(game.recipe_prototypes) do
		frame.add({type = "label", name = recipe_name, caption = recipe_name})
	end
end)

BrokenScience
Inserter
Inserter
Posts: 26
Joined: Thu Jul 21, 2016 5:31 pm
Contact:

Re: Attempt to index global 'script' (a nil value)

Post by BrokenScience »

What you have given me there crashes with the same error when in control.lua. The problem I'm having is with script in the script.on_event, not the contents (although I know it does not work). I went and deleted the entirety of the contents of the function so I had only:

Code: Select all

script.on_event("Eco", function(event)
end)
This also gives me the error. It is like script has been wiped from the game.
Smashing a brick wall with my face would be a lot more rewarding if I didn't just reveal 3 more.

Nexela
Smart Inserter
Smart Inserter
Posts: 1816
Joined: Wed May 25, 2016 11:09 am
Contact:

Re: Attempt to index global 'script' (a nil value)

Post by Nexela »

Can you post the whole mod zipped up?

User avatar
prg
Filter Inserter
Filter Inserter
Posts: 947
Joined: Mon Jan 19, 2015 12:39 am
Contact:

Re: Attempt to index global 'script' (a nil value)

Post by prg »

Can't reproduce.

data.lua:

Code: Select all

data:extend({
   {
    type = "custom-input",
    name = "Eco",
    key_sequence = "SHIFT + R",
    consuming = "script-only"
   }
})
control.lua:

Code: Select all

script.on_event("Eco", function(event)
    game.players[event.player_index].print("foo")
end)
Hit shift+r -> prints "foo".

You aren't requiring control.lua from data.lua or something like that, right?
Automatic Belt (and pipe) Planner—Automate yet another aspect of constructing your factory!

BrokenScience
Inserter
Inserter
Posts: 26
Joined: Thu Jul 21, 2016 5:31 pm
Contact:

Re: Attempt to index global 'script' (a nil value)

Post by BrokenScience »

Yes I am. No idea how that got there. (Solved)
Smashing a brick wall with my face would be a lot more rewarding if I didn't just reveal 3 more.

Post Reply

Return to “Modding help”

Who is online

Users browsing this forum: No registered users