2D idle animation disappears

Hello, Devforum

I am trying to make 2D animations, but the issue is, if I stop moving, the sprite disappears.
Here’s a video:

As you can see, the character disappears when I stop moving.

heres the main part of the code, I’ll add some more details as comments in the code:

Localscript in StarterGui

UserInputService.InputBegan:Connect(function(input)
	wait() -- if I remove this wait(), the character doesnt disappear, but it's generally glitchy.
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if char.Torso.Soul.Enabled == true then --Undertale based game
			Direction = "N" --If the direction is "N" it means that the player is currently engaged in a scene where no animations are needed
		elseif input.KeyCode == Enum.KeyCode.D then
			Direction = "R" -- "R" for right
			if IsWalking == true then
				animatewalk()
			else
				animateidle()

			end
		end
	end
	if input.UserInputType == Enum.UserInputType.Keyboard then

		if char.Torso.Soul.Enabled == true then
			Direction = "N"
		elseif input.KeyCode == Enum.KeyCode.A then
			Direction = "L"	-- "L" for left
			if IsWalking == true then
				animatewalk()
			else
				animateidle()

			end
		end
	end
	if input.UserInputType == Enum.UserInputType.Keyboard then

		if char.Torso.Soul.Enabled == true then
			Direction = "N"
		elseif input.KeyCode == Enum.KeyCode.W then
			Direction = "W"
			if IsWalking == true then
				animatewalk()
			else
				animateidle()

			end
		end
	end
	if input.UserInputType == Enum.UserInputType.Keyboard then

		if char.Torso.Soul.Enabled == true then
			Direction = "N"
		elseif input.KeyCode == Enum.KeyCode.S then
			Direction = "S"
			if IsWalking == true then
				animatewalk()
			else
				animateidle()

			end
		end
	end
	wait()
end)


If anyone could help me out, it would mean alot, thanks. I’ll provide more code upon request. Probably something really obvious that i’ve missed.

1 Like

Do any error messages appear in console when testing?

No, the output doesn’t say anything.

local players = game:GetService("Players")
players.PlayerAdded:wait()
local player = players.LocalPlayer
player.CharacterAdded:wait()
local char = player.Character

UserInputService.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if char.Torso.Soul.Enabled == true then --Undertale based game
			Direction = "N" --If the direction is "N" it means that the player is currently engaged in a scene where no animations are needed
		elseif input.KeyCode == Enum.KeyCode.D then
			Direction = "R" -- "R" for right
			if IsWalking == true then
				animatewalk()
			else
				animateidle()
			end
		end
	end
	
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if char.Torso.Soul.Enabled == true then
			Direction = "N"
		elseif input.KeyCode == Enum.KeyCode.A then
			Direction = "L"	-- "L" for left
			if IsWalking == true then
				animatewalk()
			else
				animateidle()
			end
		end
	end
	
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if char.Torso.Soul.Enabled == true then
			Direction = "N"
		elseif input.KeyCode == Enum.KeyCode.W then
			Direction = "W"
			if IsWalking == true then
				animatewalk()
			else
				animateidle()
			end
		end
	end
	
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if char.Torso.Soul.Enabled == true then
			Direction = "N"
		elseif input.KeyCode == Enum.KeyCode.S then
			Direction = "S"
			if IsWalking == true then
				animatewalk()
			else
				animateidle()
			end
		end
	end
end)

I removed the unnecessary wait() commands, and cleaned things up slightly but I didn’t make any significant changes, can you point to exactly what is not working?

If you want, I can provide you the code for the functions. If I playtest the game without the wait, the animations glitch out and aren’t as accurate, but for that, they don’t turn invisible.

Oh, I thought you added the waits to allow for the player & character to load. Keep them in then, if there’s a specific thing in the script you provided which isn’t working please describe it.

local Direction = "S"
local IsWalking = false
local UserInputService = game:GetService("UserInputService")
local plr = game.Players.LocalPlayer
local char = plr.Character
local humanoid = char:WaitForChild("Humanoid")


----------------------------------------------
----------------------------------------------

function animateidle() --function for the animations when the player is standing still
	if Direction == "W" and IsWalking == false then
		if IsWalking == false then
			for i, v in pairs(char.Torso:GetChildren()) do
				if v.ClassName == "BillboardGui" then
					v.Enabled = false
				end
			end
		wait()
			char.Torso.FaceBackStill.Enabled = true
			
			
			
		end
	end

	if Direction == "S" and IsWalking == false then
		if IsWalking == false then
			for i, v in pairs(char.Torso:GetChildren()) do
				if v.ClassName == "BillboardGui" then
					v.Enabled = false
				end
			end
wait()
			char.Torso.FaceFrontStill.Enabled = true
			
			
			
		end
	end

	if Direction == "L" and IsWalking == false then
		if IsWalking == false then
			for i, v in pairs(char.Torso:GetChildren()) do
				if v.ClassName == "BillboardGui" then
					v.Enabled = false
				end
			end
wait()
			char.Torso.FaceLeftStill.Enabled = true
			
			
			
		end
	end


	if Direction == "R" and IsWalking == false then
		if IsWalking == false then
			for i, v in pairs(char.Torso:GetChildren()) do
				if v.ClassName == "BillboardGui" then
					v.Enabled = false
				end
			end
wait()
			char.Torso.FaceRightStill.Enabled = true
			
			
			
		end
	end

end


----------------------------------------------
----------------------------------------------

function animatewalk() --function for the walkanimations
if Direction == "S" then
	if IsWalking then
		for i, v in pairs(char.Torso:GetChildren()) do
			if v.ClassName == "BillboardGui" then
				v.Enabled = false
			end
		end

		while true do

			char.Torso.FaceCameraWalk.Enabled = true
			wait(0.2)
			char.Torso.FaceCameraWalk.Enabled = false
			char.Torso.FaceFrontStill.Enabled = true
			wait(0.2)
			char.Torso.FaceFrontStill.Enabled = false
				if Direction ~= "S" or IsWalking == false then
				break
			end

		end

		end
end



if Direction == "W" then
		
	if IsWalking then
		for i, v in pairs(char.Torso:GetChildren()) do
			if v.ClassName == "BillboardGui" then
				v.Enabled = false
			end
		end

		while true do

			char.Torso.FaceAwayWalk.Enabled = true
			wait(0.2)
			char.Torso.FaceAwayWalk.Enabled = false
			char.Torso.FaceBackStill.Enabled = true
			wait(0.2)
			char.Torso.FaceBackStill.Enabled = false
				if Direction ~= "W" or IsWalking == false then
				break
			end

		end


	end
	end
	
	
	if Direction == "L" then
		if IsWalking then
			for i, v in pairs(char.Torso:GetChildren()) do
				if v.ClassName == "BillboardGui" then
					v.Enabled = false
				end
			end

			while true do

				char.Torso.FaceLeftWalk.Enabled = true
				wait(0.2)
				char.Torso.FaceLeftWalk.Enabled = false
				char.Torso.FaceLeftStill.Enabled = true
				wait(0.2)
				char.Torso.FaceLeftStill.Enabled = false
				if Direction ~= "L" or IsWalking == false then
					break
				end

			end

		end
	end
	
	if Direction == "R" then
		if IsWalking then
			for i, v in pairs(char.Torso:GetChildren()) do
				if v.ClassName == "BillboardGui" then
					v.Enabled = false
				end
			end

			while true do

				char.Torso.FaceRightWalk.Enabled = true
				wait(0.2)
				char.Torso.FaceRightWalk.Enabled = false
				char.Torso.FaceRightStill.Enabled = true
				wait(0.2)
				char.Torso.FaceRightStill.Enabled = false
				if Direction ~= "R" or IsWalking == false then
					break
				end

			end

		end
		

	end
	
end



humanoid.Running:Connect(function(speed)
	if speed > 0 then
		IsWalking = true
	else   
		IsWalking = false
	end 
end)

This is the rest of the code, the problem might also be here. I just added some comments so it’s easier to navigate through it

v.Enabled = false
Found your problem, I think. Toggling Enabled makes the entire GUI turn invisible, like a combination of disabling click detection and setting Transparency to 1 at the same time.
Try restarting the animation at speed 0 when you detect they’re standing still instead. This should make the character stand still if your walk cycle is timed correctly.

Image

It’s not really an animation, more like 2 images repeatedly getting visible and invisible.

for i, v in pairs(char.Torso:GetChildren()) do --for ALL objects inside the torso, including the children of immediate parents...
				if v.ClassName == "BillboardGui" then --Check if it's a UI container in worldspace...
					v.Enabled = false --And turn it invisible + unclickable.

The image in question isn’t the problem nor the object list, the problem is you’re turning off every image at the same time.

Yes, that is true, afterwards the code immediately reenables the needed image, I think.

Ah, I see now. I’ll leave my posts intact, just in case.

It looks like you might not be using thread yielding properly? Add an extra wait() inside the very bottom of your loop (next to v.Enabled = false) and see what happens.

1 Like

Sadly, it still doesn’t work. It just updates slower because of the wait()

I found out that the Idle function doesn’t even get executed, even though it should.

I think I know now. The problem was part of the walkanimation script. At the end it disables the image before checking if the loop should continue. I just changed it to true and then to false at the beginning of the loop.