How to make an if statement not break a loop

Hi!
I need help, because I don’t know how to make an if statement not break a loop.
Here is an example:

while true do
	wait(5)
	
	local RandomNumber = math.random(1, 10)
		
	if RandomNumber == 3 then
		print("The Number is 3")
	end
	
end

So if the number is 3 it will print the thing, but it breaks the loop. I want it to not break the loop.
Does anyone know how to do that?

any help is appreciated!

It shouldn’t break the loop? Explain to me what makes you believe it breaks the loop.

1 Like

How come this is making the loop break? this shouldn’t be occuring

1 Like

Is there something wrong in here that breaks the loop?

I’m a little confused, is Events a folder of ModuleScripts? If it is, you’re calling its method wrong. I’m sure there’s bound to be an error in the console/output if you checked it when you run the code. If there is, tell me what it is.

Is there any errors being thrown at you at all?

Events is a folder in the serverscriptservice which the modulescript is in and no there are no errors

1 Like

It doesn’t work because the folder is empty @OzeanSyler

You made a new folder in the script and the new folder is empty

What’s the code in the ModuleScripts? I’m guessing that it’s the code in those ModuleScripts that yield the loop forever.

wait thats true its a while true do loop, do you know how to fix it so it doesnt yield?

Again, show me the code you have in the ModuleScripts, and I’ll show you where the problem is.

local function spin(Subject)
	while true do
		Subject:PivotTo(Subject:GetPivot() * CFrame.Angles(0, math.rad(7), 0))
		wait(0.01)
	end
end
return spin

it would be best to surround that while loop in a task.spawn() so that it does not yield the current thread, like this:

local function spin(Subject)
	task.spawn(function()
		while true do
			Subject:PivotTo(Subject:GetPivot() * CFrame.Angles(0, math.rad(7), 0))
			wait(0.01)
		end
	end)

end
return spin

Just reminding you that it’s best not to use a while true loop, and instead use a loop that you can terminate (for example while boolVariable do, which stops when boolVariable is false).

1 Like

That is just a band-aid over an error. I wouldn’t use a modulescrip here at all.
This loop has no exit … if you mean to have it spin forever use a tween.

That is true, a ModuleScript does seem a little excessive here, the functions can simply be stored in a table in the same script as the loop.

This will spin the object in a full 360 over and over.

TweenService = game:GetService("TweenService")
spininfo = TweenInfo.new(9,Enum.EasingStyle.Linear)

--120, 240, 360
Spin1 = TweenService:Create(script.Parent,spininfo,{CFrame = script.Parent.CFrame * CFrame.Angles(math.rad(120),0,0)})
Spin2 = TweenService:Create(script.Parent,spininfo,{CFrame = script.Parent.CFrame * CFrame.Angles(math.rad(240),0,0)})
Spin3 = TweenService:Create(script.Parent,spininfo,{CFrame = script.Parent.CFrame * CFrame.Angles(math.rad(360),0,0)})

task.wait(0.33) Spin1:Play()
Spin1.Completed:Connect(function()Spin2:Play() end)
Spin2.Completed:Connect(function()Spin3:Play() end)
Spin3.Completed:Connect(function()Spin1:Play() end)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.