Animation not playing

UIS = game:GetService("UserInputService")
plr = game.Players.LocalPlayer
print(plr)

plr.CharacterAdded:Connect(function(Character)
	local Char = Character
	local Animator = Char:WaitForChild("Humanoid"):FindFirstChild("Animator")
	local Run2 = Instance.new("Animation")
	Run2.AnimationId = "rbxassetid://7422019829"
	Run2.Parent = Char
	local RunTrack2 = Animator:LoadAnimation(Run2)
	RunTrack2.Looped = true
	local Running = false
	local WS = Char.Humanoid.WalkSpeed
	

	Char.Humanoid:GetPropertyChangedSignal('WalkSpeed'):Connect(function()
		print("WS Changed")
		if WS == 30 then
			RunTrack2:Play()
		end
		if WS <= 30 then
		RunTrack2:Stop()
		end
	end)
end)

The script is detecting the humanoid and everything but when the player has a walkspeed of 30, the animation doesn’t play. why is this?

First of all, you’re defining the walkspeed variable only once when the player joins. This means that when the walkspeed does change, the walkspeed variable will still be the original value when the character was added.
Change this by defining the walkspeed variable inside the GetPropertyChangedSignal function.

Secondly, change if WS <= 30 then to if WS < 30 then. <= means less than or equal to, this is not something you want when your previous if statement is checking if walkspeed is equal to.

1 Like

What do you mean by this?, do you mean

GetPropertyChangedSignal(Char.Humanoid.WalkSpeed)

?

No, what he means is that since you defined the variable once, it will always stay like that until you redefine it. Defining the variable when it changed makes sure the variable is updated.

Char.Humanoid:GetPropertyChangedSignal('WalkSpeed'):Connect(function()
   local WS = Char.Humanoid.WalkSpeed --Gets the current walkspeed
   --Do whatever
end)
1 Like
UIS = game:GetService("UserInputService")
local players = game:GetService("Players")

players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(Character)
		local Char = Character
		local Animator = Char:WaitForChild("Humanoid"):FindFirstChild("Animator")
		local Run2 = Instance.new("Animation")
		Run2.AnimationId = "rbxassetid://7422019829"
		Run2.Parent = Char
		local RunTrack2 = Animator:LoadAnimation(Run2)
		RunTrack2.Looped = true
		local Running = false
		local Humanoid = Char.Humanoid
		Humanoid:GetPropertyChangedSignal('WalkSpeed'):Connect(function()
			local WS = Humanoid.WalkSpeed
			if WS >= 30 then
				RunTrack2:Play()
			end
			if WS <= 30 then
				RunTrack2:Stop()
			end
		end)
	end)
end)

I think this is cleaner, it’s up to you if you use it though.