I used .Died
to detect when I kill an AI but it isn't working?
1 | if game.Workspace.Enemies.Dark 5. Zombie.Died = = true then |
As long as there is a humanoid in your Zombie, you actually will want to use the died as shown below. See what happens is when a humanoid dies anything you connect to that died event will trigger and that'show we will listen to when the humanoid dies.
01 | local zombies = { } |
02 | local function setupZombie(Zombie) |
03 | local humanoid = Zombie:FindFirstChild( "Humanoid" ) |
04 | if ( not zombies [ Zombie ] ) and humanoid then |
05 | zombies [ Zombie ] = { Zombie,humanoid.Died:Connect( function () |
06 | -- Anything in here will run when the zombie dies |
07 | -- When it dies make sure to remove it from the table |
08 | if zombies [ Zombie ] then |
09 | zombies [ Zombie ] [ 1 ] :Destroy() -- Destroy automatically will disconnect() |
10 | zombies [ Zombie ] = nil -- Set the zombie index to nil so no memory |
11 | end |
12 | end ) } |
13 | end |
14 | end |
15 |
16 | for i,zombie in pairs (game.Workspace.Enemies:GetChildren()) do |
17 | setupZombie(zombie) -- This will loop every zombie into the function to set it up |
18 | end |
I just woke up to who this may concern, I kind of winged this one.
If this helped you I would greatly appreciate an upvote or solved. Good luck!
Syn above is correct in using died, and the more clunky way is to do health like so:
if script.Parent.Humanoid.Health <= 0 then
But that is slower and usually requires a while loop whereas the :died() connection does not because it listens for the death.