event on_entity_damaged leads to an error

Place to get help with not working mods / modding interface.
User avatar
WIZ4
Fast Inserter
Fast Inserter
Posts: 209
Joined: Thu Apr 07, 2016 1:36 pm
Contact:

event on_entity_damaged leads to an error

Post by WIZ4 »

I would like that when a player deals damage to the entity of another team, he receives a message.
But I get this error when I deal damage to someone else's team building

Code: Select all

script.on_event(defines.events.on_entity_damaged, function(event)
local player = game.players[event.player_index] --line 53
if event.entity.force ~= player.force then
player.print("text")
end
end)
Screenshot_5.png
Screenshot_5.png (97.96 KiB) Viewed 1526 times
Last edited by WIZ4 on Thu Mar 08, 2018 1:00 pm, edited 2 times in total.
My native language is russian. Sorry if my messages are difficult to read.
Bilka
Factorio Staff
Factorio Staff
Posts: 3470
Joined: Sat Aug 13, 2016 9:20 am
Contact:

Re: event on_entity_damaged leads to an error

Post by Bilka »

The event object doesn't contain player_index: http://lua-api.factorio.com/latest/even ... ty_damaged

You'll have to check if event.cause and event.cause.is_player(), which should only give return true if a player caused the damage.
I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.
User avatar
WIZ4
Fast Inserter
Fast Inserter
Posts: 209
Joined: Thu Apr 07, 2016 1:36 pm
Contact:

Re: event on_entity_damaged leads to an error

Post by WIZ4 »

Bilka wrote:The event object doesn't contain player_index: http://lua-api.factorio.com/latest/even ... ty_damaged

You'll have to check if event.cause and event.cause.is_player(), which should only give return true if a player caused the damage.
Oh, that's bad.thanks for the answer
My native language is russian. Sorry if my messages are difficult to read.
User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5211
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: event on_entity_damaged leads to an error

Post by eradicator »

Get the player if any, or abort if none can work like this:

Code: Select all

local player = (event.cause and event.cause.is_player()) and game.players[event.cause.player.index]
if not player then return end
Post Reply

Return to “Modding help”