Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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)

4 comments

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.

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

0

Is the countdown immediately after or is it inside the loop?

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.

1 answer

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

2 comments

0

-1 for recommending polling over events

0

This would cause insane lag over time..

This discussion is preserved as it was. New answers, comments, and votes are closed.