Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Having trouble with repeat loops? Freezing/Crashing game--Help!!!

This is a fade in and out script for a gui object, in this case it's an ImageLabel.

fade = script.Parent --The ImageLabel
fadeGoal = 0 -- What the transparency should end at. This is 0 so it is fading in.
fadeRate = 0.05 -- How fast it should fade

function updateFade()
    local current = fade.ImageTransparency --The background
    if current < fadeGoal then --If the current transparency is smaller that the the goal
        fade.ImageTransparency = math.min(fadeGoal,current+fadeRate) -- If true add the current transparency from the fade rate 
    elseif current > fadeGoal then 
        fade.ImageTransparency = math.max(fadeGoal,current-fadeRate) --If true subtract the current transparency from the fade rate 
    else
        fade.ImageTransparency = fadeGoal -- if the transparency is equel to or bigger than goal sets transparency to goal
    end
end

rs = game:GetService("RunService")

fade.Changed:Connect(function()
repeat
	fadeGoal = 1 
	until
script.Parent.ImageTransparency == 1
wait()
repeat
	fadeGoal = 0 
	until
script.Parent.ImageTransparency == 0
wait()
end)

rs.RenderStepped:connect(updateFade)




1 answer

✓ Accepted answer

You made a repeat until loop without a wait() inside: there is no guarantee that ImageTransparency will ever be 1, and so, the loop keeps running.

Since you bound updateFade after the changed event, if the label updates before renderstepped running, the loop will get stuck in an infinite loop.

Solution: add a wait() in the repeat loops.

This discussion is preserved as it was. New answers, comments, and votes are closed.