Idle and walk animation cycles not working

Hello everyone! I’m trying to make idle and walk animation cycles for my 2D character, but they don’t play when they are supposed to:

local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")

local Beam = character:WaitForChild("Texture"):WaitForChild("BeamSourceBottom"):WaitForChild("Beam")

humanoid.Running:Connect(function(speed)
	
	--Idle Animation--
	while speed == 0 do
		Beam.Texture = "rbxassetid://128582448174337"
		task.wait(1)
		Beam.Texture = "rbxassetid://139732731612701"
		task.wait(1)
	end
	
	--Walk Animation--
	while speed > 0 do
		Beam.Texture = "rbxassetid://121119574362872"
		task.wait(1)
		Beam.Texture = "rbxassetid://70787200236324"
		task.wait(1)
	end
	
end)

That’s the code. When the player is not moving, I want it to loop through the idle textures and when the player is moving then I want it to loop through the walk textures. The problem is that they both play at the same time even when the player is not moving which makes the textures keep flickering and it looks weird. The idle loop doesn’t stop when the player is moving, and the walk loop keeps playing when the player is not moving. I don’t know how I can fix this, so any help is appreciated. Thanks!

Try this and see if it works.

local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")

local Beam = character:WaitForChild("Texture"):WaitForChild("BeamSourceBottom"):WaitForChild("Beam")

local Speed = 0
local Counter = 1

local IDs = {
	{"rbxassetid://128582448174337", "rbxassetid://139732731612701"},
	{"rbxassetid://121119574362872", "rbxassetid://70787200236324"}
}

humanoid.Running:Connect(function(speed: number) 
	Speed = speed
end)

while task.wait(1) do
	local Selection = (Speed == 0) and 1 or 2
	
	Beam.Texture = IDs[Selection][Counter]
	
	Counter += 1
	Counter = (Counter > 2) and 1 or Counter
end

Edit: Ill explain it if it works (If you want to)

3 Likes

Thank you so much! This worked! And yes, you can explain it please

By the way I also wanted to know if there is a way to make the animations loop through 3 textures instead of just two. And I also want to know if the walking animation can play at a faster speed than the idle animation by having a shorter “wait” time between the textures. Thanks!

For the table, the first index is basically for idle animations, and the second index is for walking animations. Inside these table indexes, there are the animations we want to loop through.

This is the same as:

local Selection = nil
if Speed == 0 then
 Selection = 1
else 
 Selection = 2
end

Basically, if speed == 0, we’re not moving so we will select the ID table index containing the idle animations. If the speed is not 0 then we’ve moving so we will select the second index of the ID table containing the walk animations.

This part is basically our main loop part for looping through the animations. Counter +=1 basically just adds 1 to the counter. For the Counter = (Counter > 2) and 1 or Counter, if we add one to our counter and its more than to (>2) then we will reset it to 1. If its not above 2 then nothing will happen.

For this, its pretty self-explanatory but the Selection variable selects which animation type we are going to be playing and Counter selects which animation Id we should be on based on the loop

1 Like

New script for adding different wait times:

local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")

local Beam = character:WaitForChild("Texture"):WaitForChild("BeamSourceBottom"):WaitForChild("Beam")

local Speed = 0
local Counter = 1
local Max = 2

local IDs = {
	{"rbxassetid://128582448174337", "rbxassetid://139732731612701", 1},
	{"rbxassetid://121119574362872", "rbxassetid://70787200236324", .5}
}

humanoid.Running:Connect(function(speed: number) 
	Speed = speed
end)

while true do
	local Selection = (Speed == 0) and 1 or 2

	Beam.Texture = IDs[Selection][Counter]

	Counter += 1
	Counter = (Counter > Max) and 1 or Counter
	
	task.wait(IDs[Selection][Max+1])
end

You want the last index of the actual ID tables to be the wait times. For example, the wait time for the idle animations is 1. To add textures all you need to do is just put in the ID’s in the table and you probably want them to be in order. Also, change the max variable to reflect how many textures are there. For example, for 3 textures you will set the Max variable to 3.

Example of adding new textures:

local Max = 3
local IDs = {
{"ID1", "ID2", "ID3", 1 }
...
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.