Killing a character within script

Place to get help with not working mods / modding interface.
Post Reply
Jibbyb
Burner Inserter
Burner Inserter
Posts: 5
Joined: Sat Apr 29, 2017 7:29 am
Contact:

Killing a character within script

Post 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?

Nexela
Smart Inserter
Smart Inserter
Posts: 1828
Joined: Wed May 25, 2016 11:09 am
Contact:

Re: Killing a character within script

Post 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
Last edited by Nexela on Sat Apr 29, 2017 9:20 am, edited 2 times in total.

Jibbyb
Burner Inserter
Burner Inserter
Posts: 5
Joined: Sat Apr 29, 2017 7:29 am
Contact:

Re: Killing a character within script

Post 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

Nexela
Smart Inserter
Smart Inserter
Posts: 1828
Joined: Wed May 25, 2016 11:09 am
Contact:

Re: Killing a character within script

Post by Nexela »

You are correct. I goofed :) You need the character (which is an entity)

player.character.die()

Jibbyb
Burner Inserter
Burner Inserter
Posts: 5
Joined: Sat Apr 29, 2017 7:29 am
Contact:

Re: Killing a character within script

Post 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

Nexela
Smart Inserter
Smart Inserter
Posts: 1828
Joined: Wed May 25, 2016 11:09 am
Contact:

Re: Killing a character within script

Post by Nexela »

Looks like you can't kill your character, Not sure if this is intended or an oversight.

Jibbyb
Burner Inserter
Burner Inserter
Posts: 5
Joined: Sat Apr 29, 2017 7:29 am
Contact:

Re: Killing a character within script

Post by Jibbyb »

Ok, thanks for your help anyway! :)

daniel34
Global Moderator
Global Moderator
Posts: 2761
Joined: Thu Dec 25, 2014 7:30 am
Contact:

Re: Killing a character within script

Post 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?
quick links: log file | graphical issues | wiki

Jibbyb
Burner Inserter
Burner Inserter
Posts: 5
Joined: Sat Apr 29, 2017 7:29 am
Contact:

Re: Killing a character within script

Post by Jibbyb »

Yeah that was the problem, works perfectly now. Thankyou

Nexela
Smart Inserter
Smart Inserter
Posts: 1828
Joined: Wed May 25, 2016 11:09 am
Contact:

Re: Killing a character within script

Post 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

Post Reply

Return to “Modding help”