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

Script to recognise when AI died isn't working?

Asked by
zomspi 541 Moderation Voter
4 years ago

I used .Died to detect when I kill an AI but it isn't working?

if game.Workspace.Enemies.Dark5.Zombie.Died == true then

2 answers

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

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.

    local zombies = {}
    local function setupZombie(Zombie)
        local humanoid = Zombie:FindFirstChild("Humanoid")
        if (not zombies[Zombie]) and humanoid then
            zombies[Zombie] = {Zombie,humanoid.Died:Connect(function()
                -- Anything in here will run when the zombie dies
                -- When it dies make sure to remove it from the table
                if zombies[Zombie] then
                    zombies[Zombie] [1]:Destroy() -- Destroy automatically will disconnect()
                    zombies[Zombie] = nil -- Set the zombie index to nil so no memory 
                end
            end)}
        end
    end

    for i,zombie in pairs(game.Workspace.Enemies:GetChildren()) do
        setupZombie(zombie) -- This will loop every zombie into the function to set it up
    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!

  • Best Regards -Syn
1
He does not know for loops but nice tutorial man, upvoted. Gingerely 245 — 4y
0
Thanks, right back at you johndeer2233 439 — 4y
0
that didn't work, idk if I'm doing something wrong, the error said that there was something wrong with the ends zomspi 541 — 4y
0
You must of changed something. I just put everything in studio and no errors pop up. johndeer2233 439 — 4y
View all comments (4 more)
0
when I put it into studio it underlines the } on line 12 in red? zomspi 541 — 4y
0
nvm zomspi 541 — 4y
0
hang on, I'm a little lost, I think you thought that Zombie was the enemy, Dark5 is the enemy and Zombie is the humanoid, I tried to fix this but am not a great scripter, I don't really get the in pairs part zomspi 541 — 4y
0
The for part essentially sais "for everything thing in Enemies i wil be the position index and zombie will be the object itself so..." johndeer2233 439 — 4y
Ad
Log in to vote
0
Answered by
IrishFix 124
4 years ago

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.

Answer this question