You could check the Humanoid’s MoveDirection and see if it’s equal to the Vector3 0, 0, 0 or not. If it’s not, then it’s walking. If it is equal to 0, 0, 0, then it’s idle.
It works! but there is still a slight problem. I want to change and load different animations while the player is walking.Let me explain, if a player is idle and then starts moving I want a different animation to load.
its a slight problem because if a player stays idle and then try’s to move he still has the idle animation loaded in. but when a player is already moving the he has the walking animation and when he stops he dosent idle
Here is my current script:
local uis = game:GetService("UserInputService")
local plr = game.Players.LocalPlayer
local Char = plr.Character or plr.CharacterAdded:Wait()
local humanoid = Char:FindFirstChildWhichIsA("Humanoid")
-- Animations
local animTrackWalk = humanoid:LoadAnimation(game.ReplicatedStorage.Animations.AnimationCrouchWalk)
local animTrackIdle = humanoid:LoadAnimation(game.ReplicatedStorage.Animations.AnimationCrouchIdle)
-- Camera
local camera = game.Workspace.CurrentCamera
local hrp = Char:FindFirstChild("HumanoidRootPart")
uis.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.C then
repeat
if humanoid.MoveDirection == Vector3.new(0,0,0) then
camera.CameraSubject = hrp
animTrackIdle:Play()
humanoid.WalkSpeed = 8
humanoid.JumpPower = 0
else
camera.CameraSubject = hrp
animTrackIdle:Stop()
animTrackWalk:Play()
humanoid.WalkSpeed = 8
humanoid.JumpPower = 0
end
until uis.InputEnded
end
end)
uis.InputEnded:Connect(function(input)
if input.KeyCode == Enum.KeyCode.C then
camera.CameraSubject = humanoid
animTrackWalk:Stop()
animTrackIdle:Stop()
humanoid.WalkSpeed = 16
humanoid.JumpPower = 50
end
end)
it’s a slight problem because if a player stays idle and then tries to move he still has the idle animation loaded in. but when a player is already moving he has the walking animation and when he stops he doesn’t idle
I usually use a loop to check to velocity of the humanoid root part. If the absolute value of the velocity’s x or z is above something like 1 then I know it’s moving.
I still have the problem, let me explain it. I have two animations, I walking one and an idle one. when you press the key C it chooses between idle and walking because of the code. The only problem is that when I am Idle and walk the animation doesn’t switch to walking anim here is the script:
local uis = game:GetService("UserInputService")
local plr = game.Players.LocalPlayer
local Char = plr.Character or plr.CharacterAdded:Wait()
local humanoid = Char:FindFirstChildWhichIsA("Humanoid")
-- Animations
local animTrackWalk = humanoid:LoadAnimation(game.ReplicatedStorage.Animations.AnimationCrouchWalk)
local animTrackIdle = humanoid:LoadAnimation(game.ReplicatedStorage.Animations.AnimationCrouchIdle)
-- Camera
local camera = game.Workspace.CurrentCamera
local hrp = Char:FindFirstChild("HumanoidRootPart")
uis.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.C then
repeat
if humanoid.MoveDirection == Vector3.new(0,0,0) then
camera.CameraSubject = hrp
animTrackIdle:Play()
humanoid.WalkSpeed = 8
humanoid.JumpPower = 0
else
camera.CameraSubject = hrp
animTrackIdle:Stop()
animTrackWalk:Play()
humanoid.WalkSpeed = 8
humanoid.JumpPower = 0
end
until uis.InputEnded
end
end)
uis.InputEnded:Connect(function(input)
if input.KeyCode == Enum.KeyCode.C then
camera.CameraSubject = humanoid
animTrackWalk:Stop()
animTrackIdle:Stop()
humanoid.WalkSpeed = 16
humanoid.JumpPower = 50
end
end)
local player = game.Players.LocalPlayer
local human = player.Character.Humanoid
local walkanim = human:LoadAnimation(script:FindFirstChild("walkanim"))
local idleanim = human:LoadAnimation(script:FindFirstChild("idleanim"))
local RunService = game:GetService("RunService")
RunService.Heartbeat:Connect(function()
if hum.MoveDirection.Magnitude > 0 then
idleanim:Stop()
walkanim:Play()
else
walkanim:Stop()
idleanim:Play()
end
end)