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

Zombies isn't respawning after reached the limit?

Asked by 6 years ago

So I set the limit for the zombies is 3 at one spawn,but when 3 spawned and I killed 3 of them.They stopped respawning

zombie = script.Parent.Parent["Zombie1"]
zombiebackup = zombie:Clone() 
zombie:Destroy() 

local limit = 3 

for i = 1,limit do
wait(5) 
local newzombie = zombiebackup:Clone() 
newzombie.Parent = game.Workspace 
newzombie:MakeJoints()
newzombie.Torso.CFrame = script.Parent.CFrame + Vector3.new(0, 5, 0) 
newzombie:MakeJoints()
zombie = newzombie 
zombiebackup = zombie:Clone() 
end 

1 answer

Log in to vote
0
Answered by
hellmatic 1523 Moderation Voter
6 years ago

If you want them to respawn, you have to find a way to detect how many zombies there are left.

local ZombiesLeft = 0

Now, you have to create a NumberValue and put it in the script.

local ZLeftNV = Instance.new('NumberValue')
ZLeftNV.Parent = script

You have to create a function to spawn the zombies, and add 1 to ZombiesLeft after one spawns in.

local limit = 3

function spawnZombies()
zombieCheck() -- this will make a loop, so they keep respawning
    for i = 1,limit do
        wait(5) 
        ZombiesLeft = ZombiesLeft + 1 -- adds 1 when 1 zombie spawns
        ZLeftNV.Value = ZLeftNV.Value + 1 -- adds 1 to the number value when 1 zombie spawns
        local newzombie = zombiebackup:Clone() 
        newzombie.Parent = game.Workspace 
        newzombie:MakeJoints()
        newzombie.Torso.CFrame = script.Parent.CFrame + Vector3.new(0, 5, 0) 
        newzombie:MakeJoints()
        zombie = newzombie 
        zombiebackup = zombie:Clone() 
    end 
end


function zombieCheck() -- this will check if ZombiesLeft is 0, it will call the spawn function
    if ZombiesLeft == 0 then 
        spawnZombies()
    end
end

zombieCheck()

ZLeftNV.Changed:connect(function()
    ZombiesLeft = ZLeftNV.Value -- updates ZombiesLeft everytime ZLeftNV number changes
end)

Now inside the Zombie model, every time one dies, you have to decrease the ZLeftNV by 1.

--INSIDE ZOMBIE MODEL--

Humanoid = script.Parent.Humanoid

Humanoid.Died:connect(function()
    workspace.Script.ZLeftNV.Value = workspace.Script.ZLeftNV.Value - 1 -- decreases the ZLeftNV value by 1 when the zombie dies
end)
Ad

Answer this question