2d animation is buggy?

I have this code but I’ve been looking at it all day trying to optimize it so there’s no glitching or bugging when the characters changing directions or jumping.

I’ve tried a lot to fix it but it gets stuck in the idle animation sometimes and glitches/flickers when changing directions. when I jump it only play s the jump animation while I am holding down spacebar but I’m trying to make it so you have that animation anytime you’re in the air but the animations overlap, is there some way I can set a priority type of thing up? any help is appreciated, I apologize if my code is messy.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

local UserInputService = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local humanoid = player.Character:FindFirstChildOfClass("Humanoid")
local Cube = player.Character:WaitForChild("Cube")
local HumanoidRootPart = player.Character:WaitForChild("HumanoidRootPart")
local isButtonPressed = false -- Controls the loop
local IsA = false
local IsD = false
local IsSpace = false
UserInputService.InputBegan:Connect(function(input, processed)
	if not processed and input.KeyCode == Enum.KeyCode.A and IsA == false and IsD == false and IsSpace == false then		
		
		IsA = true
		IsD = false
		IsSpace = false
		isButtonPressed = true
		HumanoidRootPart.Attachment.ParticleEmitter.Rate = 5
		task.spawn(function() -- Run the loop in a separate thread
			while isButtonPressed == true and IsA == true and IsD == false and IsSpace == false do
				-- Line 1 of code
				Cube.BillboardGui.ImageLabel.Image = "rbxassetid://86484408230959"
				task.wait(0.25) -- Adjust delay as needed
				print("step")

				-- Line 2 of code
				Cube.BillboardGui.ImageLabel.Image = "rbxassetid://134049733657680"
				task.wait(0.25) -- Adjust delay as needed
				
			end
		end)
	end
	UserInputService.InputBegan:Connect(function(input, processed)
		if not processed and input.KeyCode == Enum.KeyCode.D and IsA == false and IsD == false and IsSpace == false then
			isButtonPressed = true
			IsA = false
			IsD = true
			IsSpace = false
			HumanoidRootPart.Attachment.ParticleEmitter.Rate = 5
			task.spawn(function() -- Run the loop in a separate thread
				while isButtonPressed == true and IsA == false and IsD == true and IsSpace == false do
					-- Line 1 of code
					Cube.BillboardGui.ImageLabel.Image = "rbxassetid://134185848512212"
					task.wait(0.25) -- Adjust delay as needed
					print("step")

					-- Line 2 of code
					Cube.BillboardGui.ImageLabel.Image = "rbxassetid://113551294954030"
					task.wait(0.25) -- Adjust delay as needed
					
					
				end
			end)
		end
		UserInputService.InputBegan:Connect(function(input, processed)
			if not processed and input.KeyCode == Enum.KeyCode.Space and IsA == false and IsD == false and IsSpace == false then
				isButtonPressed = true
				IsA = false
				IsD = false
				IsSpace = true
				task.spawn(function() -- Run the loop in a separate thread
					while isButtonPressed == true and IsA == false and IsD == false and IsSpace == true and humanoid.FloorMaterial == Enum.Material.Air do
						-- Line 1 of code
						Cube.BillboardGui.ImageLabel.Image = "rbxassetid://71272473484586"
						task.wait(0) -- Adjust delay as needed
						print("step")

						-- Line 2 of code
						script.Jump:Play()
						Cube.BillboardGui.ImageLabel.Image = "rbxassetid://71272473484586"
						task.wait(0) -- Adjust delay as needed
		
		end
				end)
			end
		end)
	end)
end)

UserInputService.InputEnded:Connect(function(input, processed)
	local Cube = player.Character:WaitForChild("Cube")
			if not processed and input.KeyCode == Enum.KeyCode.A then
				isButtonPressed = false
				IsA = false		
			if IsD == true then
					
			task.wait(0.25)

			-- Line 2 of code
			Cube.BillboardGui.ImageLabel.Image = "rbxassetid://113551294954030"
			
			else if IsSpace == true then
					
					task.wait(.25)
					
				-- Line 2 of code
				Cube.BillboardGui.ImageLabel.Image = "rbxassetid://113551294954030"
					
					
				end
					
				end
				--]
	end
		HumanoidRootPart.Attachment.ParticleEmitter.Rate = 0
		Cube.BillboardGui.ImageLabel.Image = "rbxassetid://139582630167371"
		wait(0.1)
		Cube.BillboardGui.ImageLabel.Image = "rbxassetid://78004517737313"
	end)

		UserInputService.InputEnded:Connect(function(input, processed)
			local Cube = player.Character:WaitForChild("Cube")
			if not processed and input.KeyCode == Enum.KeyCode.D then
				isButtonPressed = false
				IsD = false
			end
			HumanoidRootPart.Attachment.ParticleEmitter.Rate = 0
			Cube.BillboardGui.ImageLabel.Image = "rbxassetid://139582630167371"
			wait(0.1)
			Cube.BillboardGui.ImageLabel.Image = "rbxassetid://78004517737313"
			
			
		end)
		
UserInputService.InputEnded:Connect(function(input, processed)
	local Cube = player.Character:WaitForChild("Cube")
			if not processed and input.KeyCode == Enum.KeyCode.Space and humanoid.FloorMaterial ~= Enum.Material.Air then
			isButtonPressed = false
		IsSpace = false
		print("Player is on the ground")	
		Cube.BillboardGui.ImageLabel.Image = "rbxassetid://139582630167371"
		wait(0.1)
		Cube.BillboardGui.ImageLabel.Image = "rbxassetid://78004517737313"
		
	end
end)

the reason im looping it is because im trying to animate it by making the image id switch from one to another to create a choppy like animation only while that key is help, im not saure if thers a better way to do this?

Learn to make and use spritesheets (which was used back then in old time games)
so you don’t need to use replace imagelabel and instead use OffsetImageX (whatever it might i forgot) it’s in imagelabel
but it was OffsetX and OffsetY
Learn Dicitrionaries and use them, they are much better than using local IsWhateverKeycode = false

here what is Dictironaires and you can read from roblox documentation
(Recommending take advantage of it and tutorials)
and Spritesheets too

local Status={
[0]="Standing",
[1]="Air",
[2]="Moving",
[3]="Sprinting"
}

local KeyCodes={
[Enum.KeyCode.A]=2,
[Enum.KeyCode.D]=2,
[Enum.KeyCode.Space]=1
}
1 Like

thank you this makes a bit of sense, would you mind giving me an example of how to implement this in my code? also, where could i find a tutorial on sprite sheets for roblox?

You know, Youtube has got alot tutorials

basically Documentation actually explains (about Dictironaires) it well but not informative on something

here is explain of using dictironaires and putting modulescript’s function
and using dictironaires for In-game Match (this can used as information which (Status-game is in) for Server on Server Browser)

i’m not gonna teach alot so you have to self-learing

workspace:SetAttribute("v",0)
local m = require(--Your Modulescript)
-- this is status
local statusfunction = {
[0]=m.Notstarted
[1]=m.RoundisActive
[2]=m.Lost
[3]=m.Win
}

local function StatusMatch()
for i,v in statusfunction do
if workspace:GetAttribute("v") = i then -- this check if number is correct to statusfunction's dictironaires
v() -- this uses function
end
end

end

workspace:GetAttributeChanged:Connect(StatusMatch)