scripts.on_event not working

Place to get help with not working mods / modding interface.
kingarthur
Smart Inserter
Smart Inserter
Posts: 1463
Joined: Sun Jun 15, 2014 11:39 am
Contact:

scripts.on_event not working

Post by kingarthur »

im trying to set up a script that will upon an entity's death create an entity at that position. i think im close to getting it to work but im having issues with factorio telling me (attempt to index gobal 'script' a nil value). im not sure what is wrong with it as any references to script.on_event look the same as what i did.

here is the code

Code: Select all

local oretest = {}

script.on_event(defines.events.on_entity_died, function(event)
  if event.entity.name == "small-biter" then
  local smallbiter = event.entity
  local targetpos = smallbiter.surface.find_non_colliding_position("copper-ore", smallbiter.position, 10, 1)
	end
	
	local tore = player.surface.create_entity({
         name = "copper-ore", 
         position = targetPos
         })
		 end)
User avatar
prg
Filter Inserter
Filter Inserter
Posts: 947
Joined: Mon Jan 19, 2015 12:39 am
Contact:

Re: scripts.on_event not working

Post by prg »

"script" shouldn't be the problem here, if that's nil... are you actually doing this in control.lua, not data.lua? Or are you using some ancient version of Factorio?

Actual problems:
  • You're defining targetpos as a local variable in an if block, then try to use it outside that block where it's not in scope anymore.
  • Lua is case sensitive. You're defining targetpos, then try to use it as targetPos.
  • You're trying to index player, which isn't defined anywhere. Using the entity that died here would make more sense.
Automatic Belt (and pipe) Planner—Automate yet another aspect of constructing your factory!
kingarthur
Smart Inserter
Smart Inserter
Posts: 1463
Joined: Sun Jun 15, 2014 11:39 am
Contact:

Re: scripts.on_event not working

Post by kingarthur »

i was not doing it in control.lua or data.lua. i was trying to it inside entity.lua in the prototypes folder of the mod. the player reference was a mistake, as i copyed an example code and forgot to fix it. thanks
kingarthur
Smart Inserter
Smart Inserter
Posts: 1463
Joined: Sun Jun 15, 2014 11:39 am
Contact:

Re: scripts.on_event not working

Post by kingarthur »

now it works perfectly. thank you
Attachments
test.png
test.png (973.34 KiB) Viewed 2076 times
User avatar
prg
Filter Inserter
Filter Inserter
Posts: 947
Joined: Mon Jan 19, 2015 12:39 am
Contact:

Re: scripts.on_event not working

Post by prg »

kingarthur wrote:i was not doing it in control.lua or data.lua. i was trying to it inside entity.lua in the prototypes folder of the mod.
That counts as data.lua, since all those prototype definitions are included from there.
Automatic Belt (and pipe) Planner—Automate yet another aspect of constructing your factory!
kingarthur
Smart Inserter
Smart Inserter
Posts: 1463
Joined: Sun Jun 15, 2014 11:39 am
Contact:

Re: scripts.on_event not working

Post by kingarthur »

oh. well that will be good to know moving forward
Post Reply

Return to “Modding help”