Running with 2D Character Glitching

So I am working on a 2D mini-project. The character is a surface gui that uses ImageRectOffset to change the sprites via a sprite sheet. However my script glitches and I am unsure about how to do it any other way. The script is supposed to change the image when the character runs, but when the character stops it stills runs and once it starts again, it changes the image twice the speed. Here is my script:

player.Character.Humanoid.Running:Connect(function(speed)
	while true do
			wait(0.5)
			if speed <= 0 then
				isalrrunning = false
			for i,v in pairs(a:GetChildren())do
				if v:IsA("SurfaceGui")then
						v.Frame.ImageLabel.ImageRectOffset = Vector2.new(25, 2)
						break
				end
			end
			else
				if isalrrunning == false then
				isalrrunning = true
			for i,v in pairs(a:GetChildren())do
				if v:IsA("SurfaceGui")then
					v.Frame.ImageLabel.ImageRectOffset = Vector2.new(270, 0)
					wait(0.5)
				v.Frame.ImageLabel.ImageRectOffset = Vector2.new(10, 110)
				end
					end
				else
					for i,v in pairs(a:GetChildren())do
						if v:IsA("SurfaceGui")then
						v.Frame.ImageLabel.ImageRectOffset = Vector2.new(25, 2)
						end
						end
					break
					end
		end
		end
		end)

All the variables are checked and are not nil. Any help is appreciated! :grinning:

2 Likes