I made an NPC spawner script that puts a cap on how many zombies can spawn, but I can't figure out how to tell it to spawn more when the cap decreases/a player kills one, it seems to ignore ChildRemoved and stop spawning them even after I cut them from the spawner in Run mode.
local child = game.workspace.Spawn.ChildAdded:Connect(function(child) end) local abortion = game.workspace.Spawn.ChildRemoved:Connect(function(abortion) end) local ZombieCount = 0 local NPC = game.ReplicatedStorage.Zombie local spawner = game.workspace.Spawn while ZombieCount <5 do local Clone = NPC:Clone() Clone.Parent = spawner Clone.Torso.CFrame = spawner.CFrame wait(1) if child then ZombieCount = ZombieCount +1 elseif abortion then ZombieCount = ZombieCount -1 end end
Is there a better method than ChildRemoved or am I just using it wrong?
Is the counter even supposed to be in the while do loop?
If you want to spawn a zombie every time one is killed, try the following:
game.Workspace.Spawn.ChildRemoved:Connect(function() local clone = NPC:clone() clone.parent = spawner Clone.Torso.CFrame = Vector3.new(spawner.Position) ZombieCount += 1 end)
Bear in mind that this will spawn a new NPC every time any child of Spawn is destroyed or removed. If you intend to store other objects under Spawn that are not zombies, then try checking first whether it's an NPC that's been removed.