Problem with making a puzzle door

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
1 Like

You should use GetPropertyChangedSignal instead of a loop.

1 Like

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?

Yep, I was letting @anxyeity know.

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)
2 Likes

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.

1 Like

Oops, I read that as RunService.RenderStepped instead of HeartBeat. Apologies.