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

Why won't my Zombie disappear after reaching zero health?

Asked by
sheepposu 561 Moderation Voter
4 years ago
Edited 4 years ago

I have a zombie and a script to get rid of it after it has reached zero health. The only problem is that the script doesn't destroy the zombie. Note: The zombie does disappear but it takes somewhere around 10-15 seconds. Here's the script, the humanoid is named "Zombie", and the script is a direct child to the model.

local function die()
    workspace.CountDown.Value = workspace.CountDown.Value - 1
    script.Parent:Destroy()
end

script.Parent.Zombie.Died:Connect(die)
0
You are making it way harder than it actually has to be. All you need to do is check for the Died event of the Humanoid, and destroy the zombie when the event fires. Furthermore, you can put a while loop within the function that will keep looping until the countdown reaches 0. DeceptiveCaster 3761 — 4y
0
I've now updated it to use the Die event. The countdown is to countdown the amount of zombies left. So, with this I was thinking that it would just destroy itself and disappear right after it reaches 0 health. But that's not what's happening. There's always a delay before it actually kills itself, it can be from 3 to 15 seconds. Is there a way to get rid of the delay altogether? Thx in advance sheepposu 561 — 4y
0
Is the countdown immediately after or is it inside the loop? DeceptiveCaster 3761 — 4y
0
The CountDown is an int value instance in the workspace. I use it to countdown the game and after the game has started, it is used to keep track of the zombies. Once it reaches 0, the game will end and wait for enough players to restart. If that is effecting the script I can move it to a script in sss that handles a kill event. sheepposu 561 — 4y

1 answer

Log in to vote
-2
Answered by 4 years ago
Edited 4 years ago

Usually for npc I do something along the lines of

local zombie = script.Parent
local zombieHumanoid = zombie:WaitForChild("Humanoid")

while zombieHumanoid.Health > 0 do -- while zombieIsAlive do
    wait()
    -- zombie ai stuff etc
end

-- run code when zombie dies, decrease countdown, respawn etc
zombie:Destroy()
zombie = nil

This way the npc will interact and do its job, and once it dies it will break from loop and you can handle its death

0
-1 for recommending polling over events DeceptiveCaster 3761 — 4y
0
This would cause insane lag over time.. Robowon1 323 — 4y
Ad

Answer this question