OzeanSyler
(SomeRandomNoob)
April 28, 2024, 11:54am
#1
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!
TestyLike3
(TestyLike3)
April 28, 2024, 11:59am
#2
It shouldn’t break the loop? Explain to me what makes you believe it breaks the loop.
1 Like
2unkens
(Sunken)
April 28, 2024, 11:59am
#3
How come this is making the loop break? this shouldn’t be occuring
1 Like
OzeanSyler
(SomeRandomNoob)
April 28, 2024, 12:04pm
#4
Is there something wrong in here that breaks the loop?
TestyLike3
(TestyLike3)
April 28, 2024, 12:06pm
#5
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.
2unkens
(Sunken)
April 28, 2024, 12:07pm
#6
Is there any errors being thrown at you at all?
OzeanSyler
(SomeRandomNoob)
April 28, 2024, 12:09pm
#7
Events is a folder in the serverscriptservice which the modulescript is in and no there are no errors
1 Like
i6Hx
(i6Hx)
April 28, 2024, 12:09pm
#8
It doesn’t work because the folder is empty @OzeanSyler
i6Hx
(i6Hx)
April 28, 2024, 12:10pm
#9
You made a new folder in the script and the new folder is empty
TestyLike3
(TestyLike3)
April 28, 2024, 12:10pm
#10
What’s the code in the ModuleScripts? I’m guessing that it’s the code in those ModuleScripts that yield the loop forever.
OzeanSyler
(SomeRandomNoob)
April 28, 2024, 12:12pm
#11
wait thats true its a while true do loop, do you know how to fix it so it doesnt yield?
TestyLike3
(TestyLike3)
April 28, 2024, 12:12pm
#12
Again, show me the code you have in the ModuleScripts, and I’ll show you where the problem is.
OzeanSyler
(SomeRandomNoob)
April 28, 2024, 12:13pm
#13
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
TestyLike3
(TestyLike3)
April 28, 2024, 12:14pm
#14
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
2112Jay
(2112Jay)
April 28, 2024, 12:17pm
#15
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.
TestyLike3
(TestyLike3)
April 28, 2024, 12:18pm
#16
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.
2112Jay
(2112Jay)
April 28, 2024, 12:23pm
#17
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)
system
(system)
Closed
May 12, 2024, 12:24pm
#18
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.