You can use break()
to stop a while-loop and a for-loop
ābreakā will break the loop for you.
There is no task.wait()
in your while loop, it may be erroring.
No, you should be using while true do
as itās clearer and the loop is infinite as the condition is constant. Using while wait()
idiom is a hacky trick method really.
while wait() do
print("foo bar")
end
-- 2
while wait(1) do
if aMagnitude < bMagnitude then
return cSomethingProbably
end
end
-- 3
local function foo(bar)
wait(0.5)
return foobar
end
while foo(bar2) do
print("foo bar?")
end
while true do
doesnāt even work. Try it yourself. Open a game and write some code with while true do
. Roblox Studio will crash.
Obviously it will crash as it will loop infinitely very very fast and prevent other tasks of the game to run causing it to crash. You need to add a task.wait()
in it.
can you give me an example from my script?
Thatās why you use game:GetService("RunService").Heartbeat:Wait()
Itās supposed to crash if you donāt have anything stopping the condition from constantly running.
local function colorUpdated()
if (script.Parent.red.BrickColor ~= BrickColor.new("Bright orange")) then
return -- Cancel function
end
if (script.Parent.Blue.BrickColor ~= BrickColor.new("Bright blue")) then
return -- Cancel function
end
print("pp")
script.Parent.Part.Transparency = 1
script.Parent.Part.CanCollide = false
end
script.Parent.red:GetPropertyChangedSignal("BrickColor"):Connect(colorUpdated)
script.Parent.Blue:GetPropertyChangedSignal("BrickColor"):Connect(colorUpdated)
RunService
can only be accessed by a LocalScript.
Wrong, it can be accessed by server scripts as well. Only RunService.RenderStepped
cannot be accessed by a server script.
Oops, I read that as RunService.RenderStepped
instead of HeartBeat
. Apologies.