Page 1 of 1
Killing a character within script
Posted: Sat Apr 29, 2017 7:34 am
by Jibbyb
I can decrease a characters health over time but once it reaches zero the character remains on zero health without dying. Is there a method for killing the character?
Re: Killing a character within script
Posted: Sat Apr 29, 2017 8:45 am
by Nexela
Jibbyb wrote:I can decrease a characters health over time but once it reaches zero the character remains on zero health without dying. Is there a method for killing the character?
.die()
Code: Select all
if player.character.health == 1 then player.character.die() else ...apply damage.. end
Re: Killing a character within script
Posted: Sat Apr 29, 2017 9:08 am
by Jibbyb
When doing that I get this error
Code: Select all
LuaPlayer doesn't contain key die.
and in the docs there is no reference to a .die() method:
http://lua-api.factorio.com/0.15.3/LuaPlayer.html
Re: Killing a character within script
Posted: Sat Apr 29, 2017 9:19 am
by Nexela
You are correct. I goofed
You need the character (which is an entity)
player.character.die()
Re: Killing a character within script
Posted: Sat Apr 29, 2017 9:43 am
by Jibbyb
Ok, so now I get this error thrown:
Code: Select all
attempt to index field 'character' (a nil value)
by this code:
Code: Select all
for index, plyr in pairs(game.players) do
....
plyr.character.health = plyr.character.health - 1 --This line runs fine and the players health decrease incrementally
if plyr.character.health == 0 then
plyr.character.die() -- This line throws the error
end
end
Re: Killing a character within script
Posted: Sat Apr 29, 2017 10:23 am
by Nexela
Looks like you can't kill your character, Not sure if this is intended or an oversight.
Re: Killing a character within script
Posted: Sat Apr 29, 2017 10:44 am
by Jibbyb
Ok, thanks for your help anyway!
Re: Killing a character within script
Posted: Sat Apr 29, 2017 12:06 pm
by daniel34
I don't think the die() is the cause, characters can die without any problems. I think you are trying to access player.character afterwards which gives the error (after the character died it will be nil until the character respawns). Do you check if the player has a character ~= nil before accessing the health property or die() function?
Re: Killing a character within script
Posted: Sat Apr 29, 2017 12:23 pm
by Jibbyb
Yeah that was the problem, works perfectly now. Thankyou
Re: Killing a character within script
Posted: Sat Apr 29, 2017 1:00 pm
by Nexela
Hmmm There is a very good chance I was not including the /c when trying to die()
Also see above
You would want to check if player.controller_type == defines.controllers.character and player.character.valid