Idle Animation For Image Label

Hello, I’m trying to make a character bop to a beat while there’s no input, I tried using a while true do but it ended up crashing my script, I’m really stuck, can anyone help me fix this?

image

while idle == true do
	script.Parent.Image = "rbxassetid://121803704513390"
	wait(1)
	script.Parent.Image = "rbxassetid://126828287438911"
	wait(1)
end

FYI: It does work but once it starts playing, clicking (Main form of input) doesn’t work.

1 Like

It’s possible that the loop is stopping any subsequent code (including the code that sets idle to false), preventing it from behaving how you want.

If you try using task.spawn or a coroutine on the while loop, does it fix the issue?
e.g.

task.spawn(function()
	while idle == true do
		script.Parent.Image = "rbxassetid://121803704513390"
		wait(1)
		script.Parent.Image = "rbxassetid://126828287438911"
		wait(1)
	end
end)

(both options just make the code run without causing subsequent code to have to wait for the code to run to completion)

1 Like

How exactly would I specify the code? I’m kinda a noob at coding, sorry

Just copy and paste what I put in place of the code you provided initially

1 Like

Okay so… I’ve done some testing and being set to false destroys it entirely, is there anyway to get around this?

In how many places are you setting idle to true?
If it’s just one place, I would recommend putting the while loop directly after the line of code that sets idle to true

I’m placing them in functions, so should I just put it at the end of the function?

One way I’ve thought of doing this is just by having a simple loop and a variable that signifies the last time you’ve pressed something. In the loop, it detects that if you haven’t pressed anything for a set amount of seconds, it would handle the idling stuff.

local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")

local lastInput = os.clock() -- last time you've pressed something
local timeUntilIdle = 5 -- how long it takes for you to become idle

local loopStarted = false
local stopFunction = nil

local function startLoop()
    if not loopStarted then
        local stop = false
        task.spawn(function()
            while not stop do
                script.Parent.Image = "rbxassetid://121803704513390"
                task.wait(1)
                script.Parent.Image = "rbxassetid://126828287438911"
                task.wait(1)
            end
        end)

        stopFunction = function()
            stop = true
            loopStarted = false
        end
        loopStarted = true
    end
end

UserInputService.InputBegan:Connect(function()
    lastInput = os.clock()
end)

RunService.RenderStepped:Connect(function()
    if not loopStarted and os.clock() - lastInput > timeUntilIdle then
        startLoop()
    elseif loopStarted and os.clock() - lastInput <= timeUntilIdle then
        stopFunction()
    end
end)

This code should also work based off the code that you gave.

local current = 0

while true do
    if idle == true then
        if current == 0 then
            script.Parent.Image = "rbxassetid://121803704513390"
            current = 1
        else
            script.Parent.Image = "rbxassetid://126828287438911"
            current = 0
        end
        task.wait(1)
    end
    task.wait()
end