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

How do I make a script cap off at a certain amount of clones?

Asked by 6 years ago

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

1 answer

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago
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
0
Thank you so much, it works very well! KingCheese13 21 — 6y
0
Not a problem! :) WizyTheNinja 834 — 6y
0
Wait, it started, waited 80 seconds (time I set for alien spawn) , then it spawns all 10 aliens. If any are killed, it waits 80 seconds again and then spawns the amount that were killed. I would like it to wait 80 seconds, spawn one, wait 80 more seconds then spawn another one, until it reaches 10. KingCheese13 21 — 6y
0
I'm sorry. I corrected my answer with a second edit. I tested that one, and it will spawn 1 at a time aslong as its below max. WizyTheNinja 834 — 6y
0
Thanks a ton! KingCheese13 21 — 6y
Ad

Answer this question