Page 1 of 1

event on_entity_damaged leads to an error

Posted: Thu Mar 08, 2018 11:40 am
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 1525 times

Re: event on_entity_damaged leads to an error

Posted: Thu Mar 08, 2018 11:50 am
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.

Re: event on_entity_damaged leads to an error

Posted: Thu Mar 08, 2018 4:33 pm
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

Re: event on_entity_damaged leads to an error

Posted: Thu Mar 08, 2018 5:44 pm
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