Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why isn't the despawn script for my NPC not working?

Asked by 6 years ago

I have a script to despawn an npc after it dies but it does nothing Here is the code:

robot = script.Parent.Robot

if robot.Health <= 0 then
    script.Parent:Destroy()
end

BTW, Robot is just humanoid but it is renamed. Also the script is a child of the NPC. & finally, the NPC is a sort of 'zombie' that attacks the player so there are multiple clones of it in the map as well, just in case that messes anything up.

3 answers

Log in to vote
0
Answered by
zor_os 70
6 years ago

You're not checking every time the humanoids health changes you only check once.

I suggest you use

while true do
    wait(0.1) -- so it doesn't crash
    robot = script.Parent.Robot 
    if robot.Health <= 0 then
        script.Parent:Destroy()
    end
end

a change event may also work, to be honest I've never tried it so I don't know if it will activate every time its health changes but it should.

Also why isn't the despawn script not working is the same as saying why is this script working.

0
K thanks for the answer, & yeah sorry I didn't realise that I wrote the title like that. CoolOlivie057 45 — 6y
0
No problem :) title just confused me while scrolling past so thought I'd let you know zor_os 70 — 6y
Ad
Log in to vote
1
Answered by
Synth_o 136
6 years ago

Put your code in a loop so it keeps firing, right now you just have it fire once

If you don't know how to use loops then go to this website:

  • https://www.robloxdev.com/articles/Roblox-Coding-Basics-Loops

In this case I would suggest doing this:

While true do
    wait(0.1)
            -- The code you have right now goes here
end
Log in to vote
0
Answered by
Hizar7 102
6 years ago
Edited 6 years ago

You shouldnt use less then 0 ( <= 0 ) i would say < 1, Also this isn't looping or in a died function.. so it wouldn't activate

Answer this question