Killing a character within script
Killing a character within script
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
.die()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?
Code: Select all
if player.character.health == 1 then player.character.die() else ...apply damage.. end
Last edited by Nexela on Sat Apr 29, 2017 9:20 am, edited 2 times in total.
Re: Killing a character within script
When doing that I get this error
and in the docs there is no reference to a .die() method:
http://lua-api.factorio.com/0.15.3/LuaPlayer.html
Code: Select all
LuaPlayer doesn't contain key die.
http://lua-api.factorio.com/0.15.3/LuaPlayer.html
Re: Killing a character within script
You are correct. I goofed You need the character (which is an entity)
player.character.die()
player.character.die()
Re: Killing a character within script
Ok, so now I get this error thrown:
by this code:
Code: Select all
attempt to index field 'character' (a nil value)
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
Looks like you can't kill your character, Not sure if this is intended or an oversight.
Re: Killing a character within script
Ok, thanks for your help anyway!
Re: Killing a character within script
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
Yeah that was the problem, works perfectly now. Thankyou
Re: Killing a character within script
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
Also see above
You would want to check if player.controller_type == defines.controllers.character and player.character.valid