So everything is in the right place, but it won’t work. No errors, though I will actively update if there are any.
Here’s the code: (it’s a script under a model in the workspace)
local npc = script.Parent
local humanoid = npc:WaitForChild("Humanoid")
local anim = script:WaitForChild("Idle")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local startAnim = ReplicatedStorage:WaitForChild("Events"):WaitForChild("NPCAnimation")
local stopAnim = ReplicatedStorage:WaitForChild("Events"):WaitForChild("StopAnimation")
local function playIdleAnimation()
local animator = humanoid:FindFirstChildOfClass("Animator")
if not animator then
animator = Instance.new("Animator")
animator.Parent = humanoid
end
startAnim:FireAllClients(humanoid, anim)
humanoid.Running:Connect(function()
if humanoid.WalkSpeed > 0 then
stopAnim:FireAllClients(humanoid, anim)
else
startAnim:FireAllClients(humanoid, anim)
end
end)
end
playIdleAnimation()
Hmm, I’m not sure if I’m absolutely wrong right now but the walkspeed does not change while the player is running. The WalkSpeed property decides at what speed the character will move. To get the current speed of the body, you will need to use the first parameter of the .Running event. So right now your script will always fire the “stopAnim” event, as the WalkSpeed is always above 0 seemingly (which makes sense considering a value of 0 would make the character not move at all).
-- your code before here
humanoid.Running:Connect(function(speed) -- speed is the CURRENT speed of character
if speed > 0 then -- checks for current speed, not for WalkSpeed
stopAnim:FireAllClients(humanoid, anim)
else
startAnim:FireAllClients(humanoid, anim)
end
end)
--rest of code continues here
Instead of detecting humanoid.Running event, detect if the move direction changed, specifically the magnitude of the Move direction if more than 0 then the player is moving, if 0 or less then the player is idle
Humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(function()
if Humanoid.MoveDirection.Magnitude > 0 then
-- player is moving
else
-- player is idle
end
end)
I wrote this code on mobile, i apologize if theres any syntax errors