Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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.

3 comments

1

use an if statement and then put break inside it, just the word break

1

well obviously, I need to know how I can break it after a certain amount of times the loop has played

0

A for loop

1 answer

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

Links:

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

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

Hope this helps

1 comment

0

Good answer

This discussion is preserved as it was. New answers, comments, and votes are closed.