Hello! I haven’t been on DevForums in a long time for personal reasons. Anyway, I have something that is very confusing. You see, when you use the ‘break’ command inside a script, the loop doesn’t break until it’s done. Is there any solution to this? If there is, please comment down below.
local turnedbackground = false
local function colorframe()
while true do
if turnedbackground == true then
script.Parent.PlayButton.Frame.ImageColor3 = Color3.fromRGB(52, 52, 52)
break
end
script.Parent.PlayButton.Frame.ImageColor3 = Color3.fromRGB(0, 0, 255)
wait(1)
script.Parent.PlayButton.Frame.ImageColor3 = Color3.fromRGB(9, 137, 207)
wait(1)
end
end
I’m not quite sure what your asking but this is what i’m sorta getting from it. You could wrap the colorFrame function inside a spawn function so that it creates a new thread.
That is because your if statement is the first part of the actual function, It just skips the if statement when the function first starts. it would also be easier if you did:
while turnedbackground == false do
--code here
end
This is about the same problem when creating firearms with an automatic cycle and a deeper problem as it relates to the use of loops. You see, it’s in cases like this where it’s important to know what the parts of a while loop are and how exactly to best use one. You need to make use of the conditional.
-- Use variables to avoid redundancy
local playButton = script.Parent.PlayButton
local turnedBackground = false
local function colourFrame()
while turnedBackground do
-- Below can also be a variable
playButton.Frame.ImageColor3 = Color3.fromRGB(52, 52, 52)
-- Unsure of what else goes here
end
-- Whatever needs to occur after the loop?
end
-- Also worth a variable
playButton.Frame.TextButton.MouseEnter:Connect(function ()
turnedBackground = true
colourFrame()
end)
playButton.Frame.TextButton.MouseLeave:Connect(function ()
turnedBackground = false
end)
Just a heads up that you could achieve this exact same functionality using TweenService and be able to rely on events instead of loops. I’d encourage it if you’re going for a colour changing flash effect based on if the user hovers over a button or not.
If you choose to do so, what you will be aiming to do here is create a reversing tween that will run infinitely and play or stop it depending on if the mouse has hovered over your frame.
That is not the point that colbert2677 was trying to put out, he was trying to explain to everyone how you should be using while loops, and loops in general. There is also no point in giving out the whole script to someone because then they don’t learn, they just take the script without knowing anything that was suppose to be taught.
If you’re using the conditional properly, then there’s virtually no reason for you to use break. The conditional will determine when the loop should be iterating and if it’s not met, then it’ll keep going. If you need to stop a loop mid-iteration for any reason, then you can use break to jump out of the loop.
I personally never have found a need to use break with while loops but others may have different stories. For me, I use break primarily with a for loop statement if I want to finish the loop earlier than waiting for it to finish all the specified iterations.
Use of the disabled property does not belong in production code. You may find a niche case for it but there will generally always be a better way to do what you’re thinking of. Disabling in this case is not at all necessary when the simple fix is using the loop’s components properly.