Hey everyone! I’m currently making a crouching system for a game im workin on and it doesn’t work correctly. No errors in the output. Basically, if you move your character before crouching then crouch it will play the walking animation, but If you crouch then move while crouched you just stay idle. I will paste the code here. The resource tree is fine, i know that for sure.
local UIS = game:GetService(“UserInputService”)
local player = game.Players.LocalPlayer
local character = player.Character
local humanoid = character:WaitForChild(“Humanoid”)
crouched = false
moving = false
local crouchedanim = script:WaitForChild(“Crouched”)
local crouchedtrack = humanoid:LoadAnimation(crouchedanim)
local crouchmoving = script:WaitForChild(“CrouchWalk”)
local crouchmovetrack = humanoid:LoadAnimation(crouchmoving)
crouchmovetrack.Priority = Enum.AnimationPriority.Core
crouchedtrack.Priority = Enum.AnimationPriority.Core
while true do wait()
– play animation
UIS.InputBegan:Connect(function(key)
if key.KeyCode == Enum.KeyCode.LeftControl then
crouched = true
if moving == true then
–play the walk anim
crouchmovetrack.Priority = Enum.AnimationPriority.Action
crouchedtrack.Priority = Enum.AnimationPriority.Core
– makes the walking priority higher than the idles
crouchedtrack:Stop()
crouchmovetrack:Play()
– play the walk and stop the idle
crouchmovetrack.Looped = true
humanoid.WalkSpeed = 5
else
crouchmovetrack:Stop()
crouchedtrack:Play()
crouchedtrack.Looped = true
– adjust priorities again
crouchmovetrack.Priority = Enum.AnimationPriority.Core
crouchedtrack.Priority = Enum.AnimationPriority.Action
end
end
end)
UIS.InputEnded:Connect(function(key)
if key.KeyCode == Enum.KeyCode.LeftControl then
crouched = false
crouchedtrack:Stop()
crouchmovetrack:Stop()
humanoid.WalkSpeed = 10
crouchmovetrack.Priority = Enum.AnimationPriority.Core
crouchedtrack.Priority = Enum.AnimationPriority.Core
end
end)
if humanoid.MoveDirection.Magnitude > 0 then
moving = true
else
moving = false
–[[ i believe the error is here. i think i might know what it is, however. I think it could possibly be that we’re not checking if the player
is crouched already?? maybe another thing for the script to check? idk i need sleep lol ]]
end
end
local UIS = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local character = player.Character
local humanoid = character:WaitForChild("Humanoid")
crouched = false
moving = false
local crouchedanim = script:WaitForChild("Crouched")
local crouchedtrack = humanoid:LoadAnimation(crouchedanim)
local crouchmoving = script:WaitForChild("CrouchWalk")
local crouchmovetrack = humanoid:LoadAnimation(crouchmoving)
crouchmovetrack.Priority = Enum.AnimationPriority.Core
crouchedtrack.Priority = Enum.AnimationPriority.Core
while true do
wait()
--play animation
UIS.InputBegan:Connect(function(key)
if key.KeyCode == Enum.KeyCode.LeftControl then
crouched = true
if moving == true then
--play the walk anim
crouchmovetrack.Priority = Enum.AnimationPriority.Action
crouchedtrack.Priority = Enum.AnimationPriority.Core
-- makes the walking priority higher than the idles
crouchedtrack:Stop()
crouchmovetrack:Play()
-- play the walk and stop the idle
crouchmovetrack.Looped = true
humanoid.WalkSpeed = 5
else
crouchmovetrack:Stop()
crouchedtrack:Play()
crouchedtrack.Looped = true
-- adjust priorities again
crouchmovetrack.Priority = Enum.AnimationPriority.Core
crouchedtrack.Priority = Enum.AnimationPriority.Action
end
end
end)
UIS.InputEnded:Connect(function(key)
if key.KeyCode == Enum.KeyCode.LeftControl then
crouched = false
crouchedtrack:Stop()
crouchmovetrack:Stop()
humanoid.WalkSpeed = 10
crouchmovetrack.Priority = Enum.AnimationPriority.Core
crouchedtrack.Priority = Enum.AnimationPriority.Core
end
end)
if humanoid.MoveDirection.Magnitude > 0 then
moving = true
else
moving = false
--[[
i believe the error is here. i think i might know what it is, however. I think it could possibly be that we’re not checking if the player
is crouched already?? maybe another thing for the script to check? idk i need sleep lol
]]
end
end
You can format your code like this by using a triplet of backticks (` to the left of 1) above and below the pasted code.
You could use the code inside the UIS connection to describe what should happen while the player is crouching, and after finding the new state for moving/crouching, switch animations using the already-existing crouching/moving variables if they changed.
-- keep all the code from before the while loop
while true do
wait() -- wait 1/30 second
-- is the player crouching this step?
local nowCrouched = UIS:IsKeyDown(Enum.KeyCode.LeftControl)
-- is the player moving this step?
local nowMoving = humanoid.MoveDirection.Magnitude > 0
if nowCrouched == crouched and (not nowCrouched or nowMoving == moving) then
continue
end
-- I just copied and pasted code for deciding what to do if the variables change
if nowCrouched then
if nowMoving then
--play the walk anim
crouchmovetrack.Priority = Enum.AnimationPriority.Action
crouchedtrack.Priority = Enum.AnimationPriority.Core
-- makes the walking priority higher than the idles
crouchedtrack:Stop()
crouchmovetrack:Play()
-- play the walk and stop the idle
crouchmovetrack.Looped = true
humanoid.WalkSpeed = 5
else
crouchmovetrack:Stop()
crouchedtrack:Play()
crouchedtrack.Looped = true
-- adjust priorities again
crouchmovetrack.Priority = Enum.AnimationPriority.Core
crouchedtrack.Priority = Enum.AnimationPriority.Action
end
else
crouchedtrack:Stop()
crouchmovetrack:Stop()
humanoid.WalkSpeed = 10
crouchmovetrack.Priority = Enum.AnimationPriority.Core
crouchedtrack.Priority = Enum.AnimationPriority.Core
end
crouched = nowCrouched
moving = nowMoving
end
There might be bugs here, I haven’t tested it or anything.
Things I changed:
I removed the UIS event connections entirely since it would be easier to use UIS:IsKeyDown.
There probably is a way to use all events instead of a loop by connecting to humanoid:GetPropertyChangedSignal("MoveDirection"), and then putting the if statement with all the animation logic in a function that’s called whenever our variables change.
I moved all the UIS code into an if statement that sets everything correctly.