Animation only plays once when activated?

I want this script to have the animation loop or keep going, it is a walking animation but it still only plays once, cant figure out why?

The script

game.Players.PlayerAdded:Connect(function (plr)
	plr.CharacterAdded:Connect(function (char)
		local humanoid = char:WaitForChild("Humanoid")
		local track = humanoid:LoadAnimation(game.ReplicatedStorage.Animations.Walk)
		track.Priority = Enum.AnimationPriority.Movement

		humanoid.Running:Connect(function (speed)
			if speed > 0 then
				track.Looped = true
				if not track.IsPlaying then
					track:Play()
				end
			else
				track:Stop()
			end
		end)
	end)
end)

put the track.looped outside the scope

game.Players.PlayerAdded:Connect(function (plr)
	plr.CharacterAdded:Connect(function (char)
		local humanoid = char:WaitForChild("Humanoid")
		local track = humanoid:LoadAnimation(game.ReplicatedStorage.Animations.Walk)
		track.Priority = Enum.AnimationPriority.Movement
		track.Looped = true 

		humanoid.Running:Connect(function (speed)
			if speed > 0 then
				if not track.IsPlaying then
					track:Play()
				end
			else
				track:Stop()
			end
		end)
	end)
end)

i did that while i was waiting for replies but it still wont work

can you give me the output

print("Script started")

game.Players.PlayerAdded:Connect(function (plr)
    print("Player added: " .. plr.Name)
    
    plr.CharacterAdded:Connect(function (char)
        print("Character added for: " .. plr.Name)
        
        local humanoid = char:WaitForChild("Humanoid")
        print("Humanoid found for: " .. plr.Name)
        
        local animationInstance = game.ReplicatedStorage.Animations.Walk
        print("Animation instance: " .. tostring(animationInstance))
        
        local track = humanoid:LoadAnimation(animationInstance)
        print("Animation loaded for: " .. plr.Name)
        
        track.Priority = Enum.AnimationPriority.Movement
        track.Looped = true
        print("Animation set to loop")

        humanoid.Running:Connect(function (speed)
            print("Running event fired. Speed: " .. speed)
            
            if speed > 0 then
                if not track.IsPlaying then
                    print("Playing animation for: " .. plr.Name)
                    track:Play()
                end
            else
                print("Stopping animation for: " .. plr.Name)
                track:Stop()
            end
        end)
    end)
end)

print("Script finished loading")
1 Like

Script started - Server - Animations:1
Script finished loading - Server - Animations:38
Player added: WindyTundra - Server - Animations:4
Character added for: WindyTundra - Server - Animations:7
Humanoid found for: WindyTundra - Server - Animations:10
Animation instance: Walk - Server - Animations:13
Animation loaded for: WindyTundra - Server - Animations:16
Animation set to loop - Server - Animations:20
Running event fired. Speed: 0 - Server - Animations:23
Stopping animation for: WindyTundra - Server - Animations:31
Running event fired. Speed: 0 - Server - Animations:23
Stopping animation for: WindyTundra - Server - Animations:31

I would recommend you use animation editor to save/publish the animation as a Looped animation then you won’t need to use a script to make it loop.

But as said before setup the loop after loading animation

local track = humanoid:LoadAnimation(game.ReplicatedStorage.Animations.Walk)
track.Looped = true
track:Play()
1 Like

did that but nothing changed
game.Players.PlayerAdded:Connect(function (plr)
plr.CharacterAdded:Connect(function (char)
local humanoid = char:WaitForChild(“Humanoid”)
local track = humanoid:LoadAnimation(game.ReplicatedStorage.Animations.Walk)
track.Priority = Enum.AnimationPriority.Movement
track.Looped = true

	humanoid.Running:Connect(function (speed)
		if speed > 0 then
			if not track.IsPlaying then
				track:Play()
			end
		else
			track:Stop()
		end
	end)
end)

end)

yeah it’s because the running could cause the issue we gonna try statechanged and movedirection could you give it a try

print("Script started")

game.Players.PlayerAdded:Connect(function (plr)
    print("Player added: " .. plr.Name)
    
    plr.CharacterAdded:Connect(function (char)
        print("Character added for: " .. plr.Name)
        
        local humanoid = char:WaitForChild("Humanoid")
        print("Humanoid found for: " .. plr.Name)
        
        print("Initial WalkSpeed: " .. humanoid.WalkSpeed)
        humanoid.WalkSpeed = 16  -- أو أي قيمة تناسبك
        print("WalkSpeed set to: " .. humanoid.WalkSpeed)
        
        local animationInstance = game.ReplicatedStorage.Animations.Walk
        print("Animation instance: " .. tostring(animationInstance))
        
        local track = humanoid:LoadAnimation(animationInstance)
        print("Animation loaded for: " .. plr.Name)
        
        track.Priority = Enum.AnimationPriority.Movement
        track.Looped = true
        print("Animation set to loop")

        track.Stopped:Connect(function()
            print("Animation stopped for: " .. plr.Name)
        end)

        track.Played:Connect(function()
            print("Animation started playing for: " .. plr.Name)
        end)

        humanoid.StateChanged:Connect(function(oldState, newState)
            print("Humanoid state changed from " .. tostring(oldState) .. " to " .. tostring(newState))
            
            if newState == Enum.HumanoidStateType.Running then
                local speed = humanoid.WalkSpeed
                print("Current walk speed: " .. speed)
                
                if speed > 0 then
                    if not track.IsPlaying then
                        print("Playing animation for: " .. plr.Name)
                        track:Play()
                    end
                else
                    print("Stopping animation for: " .. plr.Name)
                    track:Stop()
                end
            end
        end)

        humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(function()
            local moveDirection = humanoid.MoveDirection
            print("MoveDirection changed: " .. tostring(moveDirection))
            
            if moveDirection.Magnitude > 0 then
                if not track.IsPlaying then
                    print("Playing animation due to movement for: " .. plr.Name)
                    track:Play()
                end
            else
                print("Stopping animation due to no movement for: " .. plr.Name)
                track:Stop()
            end
        end)
    end)
end)

print("Script finished loading")
1 Like

Here is a manual method that should work:

game.Players.PlayerAdded:Connect(function(plr)
    plr.CharacterAdded:Connect(function(char)
        local humanoid = char:WaitForChild("Humanoid")
        local walkAnimation = game.ReplicatedStorage.Animations:WaitForChild("WalkAnimation")
        local track = humanoid:LoadAnimation(walkAnimation)
        track.Priority = Enum.AnimationPriority.Movement
        -- Function to manually loop the animation
        local function loopAnimation()
            if track.IsPlaying then
                track:Stop()  -- Stop the current animation
            end
            track:Play()  -- Play it again
        end
        track.Stopped:Connect(function()
            loopAnimation()
        end)
        -- Connect to the humanoid running event
        humanoid.Running:Connect(function(speed)
            if speed > 0 then
                if not track.IsPlaying then
                    loopAnimation()  -- Manually start and loop
                end
            else
                track:Stop()
            end
        end)
    end)
end)

im publishing it again but looped, how do i change the property to publish it looped? sorry, dont work with animations much.

Try setting it to looped before running the :Conenct function.

game.Players.PlayerAdded:Connect(function (plr)
	plr.CharacterAdded:Connect(function (char)
		local humanoid = char:WaitForChild("Humanoid")
		local track = humanoid:LoadAnimation(game.ReplicatedStorage.Animations.Walk)
		track.Priority = Enum.AnimationPriority.Movement
		track.Looped = true
		humanoid.Running:Connect(function (speed)
			if speed > 0 then
				if not track.IsPlaying then
					track:Play()
				end
			else
				track:Stop()
			end
		end)
	end)
end)

Try clicking this button before you save:

You may also change animation priority as well:
image

1 Like

yeah i got it figured out, thanks