So I'm making a spawner that spawns different types of zombies, and I've got the random zombies and spawning down, but I need to know how to change the wait time every time it loops.
Here's my script:
local zombieChoice = math.random(1, 5) while true do math.randomseed(tick()) wait(math.random(1, 5)) if zombieChoice == 1 then zombieChoice = math.random(1, 5) local zombie = game.Lighting.Zombie1:clone() zombie.Parent = game.Workspace.Zombies zombie:MoveTo(script.Parent.Position) zombie:MakeJoints() elseif zombieChoice == 2 then zombieChoice = math.random(1, 5) local zombie = game.Lighting.Zombie2:clone() zombie.Parent = game.Workspace.Zombies zombie:MoveTo(script.Parent.Position) zombie:MakeJoints() elseif zombieChoice == 3 then zombieChoice = math.random(1, 5) local zombie = game.Lighting.Zombie3:clone() zombie.Parent = game.Workspace.Zombies zombie:MoveTo(script.Parent.Position) zombie:MakeJoints() elseif zombieChoice == 4 then zombieChoice = math.random(1, 5) local zombie = game.Lighting.HallowZombie1:clone() zombie.Parent = game.Workspace.Zombies zombie:MoveTo(script.Parent.Position) zombie:MakeJoints() elseif zombieChoice == 5 then zombieChoice = math.random(1, 5) local zombie = game.Lighting.HallowZombie2:clone() zombie.Parent = game.Workspace.Zombies zombie:MoveTo(script.Parent.Position) zombie:MakeJoints() end end
You don't really need to care about the part that spawns the zombies. The part I'm having trouble with is the wait time between spawning.
This does choose a random spawn time, but only once. It chooses a random time, then sticks to that time. So sometimes it's spitting out a zombie a second and other times it's waiting a while, but either way, it's staying on the same wait time and not changing it every time a zombie spawns. I've tried using a variable that changes every time the zombie spawns, but that ends up with the same result. How would I make it so the wait time changes every time the loop repeats? Thanks!
You only need to set random seed once, example of the problem:-
while wait(1) do math.randomseed(tick()) print(math.random(1,5)) end
Simply move the random seed outside of the loop:-
math.randomseed(tick()) while wait(1) do print(math.random(1,5)) end
There are two other issues here 1st you should not be using Lighting to store objects, you can use ReplicatedStorage.
The last problem is the amount of duplicate code you have used. The same processes are done for all zombies.
The solution:-
math.randomseed(tick()) local zombieChoice = math.random(1, 5) while true do local zombie = nil if zombieChoice == 1 then zombie = [new location here]:clone() elseif zombieChoice == 2 then zombie = [new location here]:clone() elseif zombieChoice == 3 then zombie = [new location here]:clone() elseif zombieChoice == 4 then zombie = [new location here]:clone() elseif zombieChoice == 5 then -- could just use else zombie = [new location here]:clone() end -- best to do this 1st zombie:MakeJoints() zombie.Parent = game.Workspace.Zombies zombie:MoveTo(script.Parent.Position) wait(math.random(1, 5)) zombieChoice = math.random(1, 5) end
This code removed a lot of the duplicated code. I hope this helps please comment if you do not understand how / why this code works.