I would like to make this script cap at 10 aliens, but when some die, continue to 10 again. What would be the most efficient way of doing so?
zombie = script.Parent.Parent.Alien zombiebackup = zombie:Clone() zombie:remove() while true do wait(55) 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
Alien = game.ReplicatedStorage.Alien -- Define alien AlienLimit= 10 -- Max number of aliens AlienCount = 1 -- Dont touch, this is the starting Alien count Alienfolder = Instance.new("Folder", game.Workspace) -- Creates the parent for the aliens Alienfolder.Name = "Aliens" -- Folder name while true do wait(.1) -- How fast aliens spawn. Changing this to a higher number will slow down spawns for i = AlienCount, AlienLimit do -- This is the loop. Runs below script repeatedly until count = max Alienclone = Alien:Clone() -- Clone alien Alienclone.Parent = Alienfolder -- Define alien parent AlienCount = AlienCount + 1 -- Adds one to count Alienclone.Humanoid.Died:Connect(function() -- Detects if alien dies, if so run below script. Insure alien has humanoid object AlienCount = AlienCount - 1 -- Removes one from count end) end end
Tested and works. If you place this script in workspace, and the alien model in ReplicatedStorage, it will spawn 10 zombies. For each one that dies one more will spawn. Change 'AlienLimit = 10' to whatever number you want as the limit.
Edit: Didn't consider that you may have spawn locations for the zombies. Or may want to control where they spawn. If so, use this script.
Alien = script.Parent.Alien -- Define Alien Alien.Parent = game.ReplicatedStorage -- Put alien in replicated storage. AlienLimit= 10 -- Max number of aliens AlienCount = 1 -- Dont touch, this is the starting Alien count Alienfolder = Instance.new("Folder", game.Workspace) -- Creates the parent for the aliens Alienfolder.Name = "Aliens" -- Folder name while true do wait(.1) -- How fast aliens spawn. Changing this to a higher number will slow down spawns for i = AlienCount, AlienLimit do -- This is the loop. Runs below script repeatedly until count = max Alienclone = Alien:Clone() -- Clone alien Alienclone.Parent = Alienfolder -- Define alien parent AlienCount = AlienCount + 1 -- Adds one to count Alienclone.Humanoid.Died:Connect(function() -- Detects if alien dies, if so run below script. Insure alien has humanoid object AlienCount = AlienCount - 1 -- Removes one from count end) end end
If you use the second script, create a brick and put the script inside of it. Put the alien model inside of the brick, and place it ontop of the brick.
Edit2: Updated code to spawn 1 at a time.
Alien = script.Parent.Alien -- Define Alien Alien.Parent = game.ReplicatedStorage -- Put alien in replicated storage. AlienLimit= 10 -- Max number of aliens AlienCount = 1 -- Dont touch, this is the starting Alien count. If set to 0, 11 will spawn instead of 10 Alienfolder = Instance.new("Folder", game.Workspace) -- Creates the parent for the aliens Alienfolder.Name = "Aliens" -- Folder name while true do wait(10) -- How fast aliens spawn. Changing this to a higher number will slow down spawns if AlienCount < AlienLimit then -- If there are less zombies than the limit, then run below script. Alienclone = Alien:Clone() -- Clone alien Alienclone.Parent = Alienfolder -- Define alien parent AlienCount = AlienCount + 1 -- Adds one to count print(AlienCount - 1) -- Just testing purposes. It subtracts one because original aliencount = 1 when its really 0. end Alienclone.Humanoid.Died:Connect(function() -- Detects if alien dies, if so run below script. Insure alien has humanoid object AlienCount = AlienCount - 1 -- Removes one from count print(AlienCount - 1) -- Just testing purposes. It subtracts one because original aliencount = 1 when its really 0. end) end