Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 6 years ago
Edited 6 years ago

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

Log in to vote
0
Answered by 6 years ago

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.

Ad

Answer this question