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

How do I make a random wait time change every time a loop repeats?

Asked by 7 years ago

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!

0
I don't see anything wrong. User#11440 120 — 7y

1 answer

Log in to vote
0
Answered by 7 years ago

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.

0
So I have 4 spawners in my game and I want them all to have random spawn times. This code, and pretty much any version of my original code I write with randomseed makes all 4 spawners spawn zombies at the exact same time, and this code made them all spawn the same type of zombie, then keep spawning that type of zombie, too! How do I fix this? Thanks! WesleyTheSkunk 12 — 7y
0
Then add some prints in to find the value/s and check where the problem lies. User#5423 17 — 7y
Ad

Answer this question