How to play animation after waiting for a while

Basically, when no inputs are pressed for a certain while, an animation plays. Kinda like in sonic the hedgehog. Anyone have any ideas on how to do this?

you can replace idle2 in the players animations

The issue with that is, it doesn’t play the animation after a specific time, it just randomly replaces idle1 sometimes. What I want is like in sonic the hedgehog, where when you wait several seconds, he begins impatiently tapping his foot.

you could start tracking the time the player has been standing still once there are no inputs being pressed or your character has completely stopped moving and has no velocity, and after a certain amount of time has passed, play the animation!

local LastInputTime = tick()
UserInputService.InputBegan:Connect(function()
        LastInputTime = tick()
end)
UserInputService.InputChanged:Connect(function()
        LastInputTime = tick()
end)
UserInputService.InputEnded:Connect(function()
        LastInputTime = tick()
end)
local TimeUntilIdleRuns = 5 -- seconds
local RunningIdleAnimation = false
RunService.Heartbeat:Connect(function()
        if RunningIdleAnimation then return end
        if tick() - LastInputTime >= TimeUntilIdleRuns then
               RunningIdleAnimation = true
               IdleAnimation:Play()
               task.wait(IdleAnimation.Length)
               LastInputTime = tick()
               RunningIdleAnimation = false
        end
end)

Some pseduocode like this could work

1 Like