Search code examples
while-loopluaroblox

I keep getting an unexplained error in my While loop in Lua


I am new to lua but not new to coding and I was trying to just make some code to change the Transparency of a brick as a first project in lua. But whenever I run the code I get an unexplained error in my while loop. I've tried changing everything inside the loop multiple times but it still only gives me this error.

This is the code:

value_list = {0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1,0.9,0.8,0.7,0.6,0.5,0.4,0.3,0.2,0.1,0}
index = 0
while true do
    value = value_list[index]
    game.Workspace.Brick.Transparency = value
    if index == 21 then
        index = 0
    else
        index += 1
    end
end

The code is supposed to keep changing the transparency of the object but instead it just gives me an error on line 3

(below is a screenshot of the error it gives me)

https://i.sstatic.net/0TnYD.png


Solution

  • The issue, as others have also pointed out, is that your while-loop never yields so the script is timing out. Adding a wait() inside the loop solves the problem.

    But the whole reason you're using a while loop at all is to animate a blink effect. Rather than using a while loop to step over animation values, a more powerful solution would be to use TweenService to animate the values. If you know the start and end values, TweenService will create the in-be"tween" values for you. And, you can specify that a Tween should reverse once it completes, so you can get that 0 -> 1 -> 0 blink style animation. This allows you to focus on how the animation should look, and not have to worry about the math to interpolate from the starting and ending points.

    The only tricky part is to get it to loop infinitely, but we can just listen for when the animation finishes and then create a new one.

    Try something like this :

    local TweenService = game:GetService("TweenService")
    
    -- find the brick
    local Brick = game.Workspace.Brick
    
    -- specify some details about how the animation should look
    local animationInfo = TweenInfo.new(3.0, -- animation length (seconds)
        Enum.EasingStyle.Linear, --how even is every step along the way?
        Enum.EasingDirection.InOut, -- should large increments be frontloaded or backloaded (ignored when style is Linear)
        0, -- repeat count
        true, -- does it reverse when makes it to the end value?
        0.0 -- delay (seconds)
    )
    -- specify all of the properties to animate
    local propsToTween = {
        Transparency = 1.0,
    }
    
    -- keep a reference to the currently running animation
    local currentTween
    local function createNewTween()
        -- override the existing tween
        currentTween = TweenService:Create(Brick, animationInfo, propsToTween)
    
        -- listen for when the animation completes to loop infinitely
        local animationCompletedConnection
        animationCompletedConnection = currentTween.Completed:Connect(function(playbackState)       
            if playbackState == Enum.PlaybackState.Completed or playbackState == Enum.PlaybackState.Cancelled then
                -- remove this callback
                animationCompletedConnection:Disconnect()
    
                -- create a new tween to loop
                createNewTween()
            end
        end)
    
        -- play the animation
        currentTween:Play()
    end
    
    -- start the loop
    createNewTween()