How to go about animating an ImageLabel

in my game, i have it so the server automatically puts a BillboardGui into the player. within it, it has an ImageLabel that changes upon the player moving. right now, this is the script:

local chara = script.Parent
local billboard = chara:FindFirstChild("HumanoidRootPart"):FindFirstChild("charaGui")
local hum = chara:FindFirstChild("Humanoid")
local runServ = game:GetService("RunService")

local states = {
	running = false,
	jumping = false,
	idle = false,
	sitting = false
}

hum.Running:Connect(function(speed)
	if speed > 0.1 then
		states.running = true
		states.idle = false
	elseif states.jumping == false and states.sitting == false then
		states.running = false
		states.idle = true
	end
end)
hum.Seated:Connect(function(sat)
	if sat then
		states.idle = false
		states.running = false
		states.jumping = false
		states.sitting = true
	else
		states.sitting = false
	end
end)
hum.FreeFalling:Connect(function()
	repeat wait() until hum.FloorMaterial ~= Enum.Material.Air
	states.jumping = false
end)
while true do
	wait()
	if states.running and not (states.jumping and states.idle and states.sitting) then
		while states.running and not (states.jumping and states.sitting) do
			local frame = 0
			repeat
				if states.running then
					billboard.chara.ImageRectOffset = Vector2.new((billboard.chara:GetAttribute("spriteX")*frame) + (frame*2), billboard.chara.ImageRectOffset.Y)
					wait(0.17)
					frame = frame + 1
				else break
				end
			until frame == 4
		end
	elseif states.idle then
		billboard.chara.ImageRectOffset = Vector2.new(0, billboard.chara.ImageRectOffset.Y)
	end
end

right now, i feel like this is pretty inefficient, especially in regards to wait(). i’ve read that wait() is an unreliable yielding function due to various circumstances. if a server is particularly active with lots of players doing lots of things, it would slow down. i’ve seen reccomendations to use something like RenderedStep or something. however, i’ve noticed that seems to fire at EVERY frame.

how would i go about implementing something like RenderedStep if i go that route? is it even the route i should go?

Edit: i was looking into it and it seems if i want to use RenderedStep, it would need to be in a localscript. is there a way to use something like that in a normal script?

Renderstepped would be best. Honestly you should be doing almost anything gui related in local scripts and just communicate with remote events.

1 Like

Just use Humanoid.StateChanged. And then check for Enum.HumanoidStateType.Running

my only issue with this is HumanoidStateType.Running tends to be a little unreliable in my experience, sometimes firing and sometimes not. plus, when it does fire, it fires a LOT. it’s been a while so i may be misremembering