2

# Break a loop after it runs after a certain amount of times?

I'm working on a basic wave spawner, I need this loop to end after it runs a certain amount of time.

local spawnamount = game.Workspace.Live.spawnamount.Value
local multiplier = 20 -- the amount that will eventually add onto the total amount after a wave.
local shrieker = game.ReplicatedStorage.Shrieker

while true do wait(1)
local shriekerclone = shrieker:Clone()
shriekerclone.Parent = workspace
shriekerclone.HumanoidRootPart.Position = game.Workspace.spawnpart.Position

end


have no idea what to do here, I can usually find something on the wiki to help me but not today so.

1
use an if statement and then put break inside it, just the word break greatneil80 2641 — 1y
1
well obviously, I need to know how I can break it after a certain amount of times the loop has played Masmixel 21 — 1y
0
A for loop malachiRashee 24 — 1y

2
Edited 1 year ago

Running it for a certain amount of time...you can use a variable and add onto every second, and then use an if then statement to determine if the variable is the amount of seconds that have passed, then using break to break the loop

For example;

local spawnamount = game.Workspace.Live.spawnamount.Value
local multiplier = 20 -- the amount that will eventually add onto the total amount after a wave.
local shrieker = game.ReplicatedStorage.Shrieker
local seconds = 0 -- your new variable

while true do
wait(1)
seconds = seconds + 1 -- adding a second every time
if seconds == 6 then -- lets say you want this to go on for 6 seconds
break -- breaks the loops
else -- its not at desired time
local shriekerclone = shrieker:Clone()
shriekerclone.Parent = workspace
shriekerclone.HumanoidRootPart.Position = game.Workspace.spawnpart.Position
end
end


Though I took the format you used, while true do, you can also use the repeat - until loop

Example;

local spawnamount = game.Workspace.Live.spawnamount.Value
local multiplier = 20 -- the amount that will eventually add onto the total amount after a wave.
local shrieker = game.ReplicatedStorage.Shrieker
local seconds = 0 -- your new variable

repeat
seconds = seconds + 1 -- adding a second every time
local shriekerclone = shrieker:Clone()
shriekerclone.Parent = workspace
shriekerclone.HumanoidRootPart.Position = game.Workspace.spawnpart.Position
wait(1)
until seconds == 6 -- repeating until variable "seconds" = 6


https://developer.roblox.com/en-us/articles/Conditional-Statements-in-Lua

https://developer.roblox.com/en-us/articles/Loops

Hope this helps

0
Good answer Xapelize 2602 — 1y