Hello developers! In a game I’m working on, there is a crouch animation. This is the code:
local player = game.Players.LocalPlayer
local Character = player.Character or player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local UIS = game:GetService("UserInputService")
local Animation = Instance.new("Animation")
Animation.AnimationId = "rbxassetid://7120081479"
Animation.Parent = player.Character
local Animate = true
local debounce = false
UIS.InputBegan:Connect(function(Input)
if Input.KeyCode == Enum.KeyCode.LeftControl then
if not debounce then
debounce = true
Animate = Humanoid:LoadAnimation(Animation)
Animate:Play()
Humanoid.WalkSpeed = Humanoid.WalkSpeed - 9
Humanoid.JumpPower = 0
else
Animate:Stop()
Humanoid.WalkSpeed = 16
Humanoid.JumpPower = 50
debounce = false
end
end
end)
My issue is this:
Whenever I stop moving in the crouch position, the animation stops, en when I start moving when in the crouch position, the same happens.
It’s a problem with the animation I think, because the speed says as it should when the animation glitches. (The animation is a test, it’s a single keyframe on the torso, which repeats itself)
local player = game.Players.LocalPlayer
local Character = player.Character or player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local UIS = game:GetService("UserInputService")
local crouchId = "rbxassetid://7120081479"
local walkId = "rbxassetid://913402848"
local Animate = true
local debounce = false
UIS.InputBegan:Connect(function(Input)
if Input.KeyCode == Enum.KeyCode.LeftControl then
if not debounce then
debounce = true
local lastWalkspeed = Humanoid.WalkSpeed
Humanoid.WalkSpeed = 0
Character.Animate.walk.WalkAnim.AnimationId = crouchId
Humanoid.WalkSpeed = lastWalkspeed - 9
Humanoid.JumpPower = 0
else
Humanoid.WalkSpeed = 0
Character.Animate.walk.WalkAnim.AnimationId = walkId
Humanoid.WalkSpeed = 16
Humanoid.JumpPower = 50
debounce = false
end
end
end)
if it doesnt work try changing walk.WalkAnim to run.RunAnim
this is what i use for my running/crouching system
Pretty sure the debounce is the issue since you’re using an if and else statement. Basically when the debounce runs the first time it won’t ever be turned back to false since the “debounce = false” is in the else section which was basically voided meaning that the next time you would click LeftControl it would skip the “if not debounce then” section and go straight to else which would break the animate and it just continues in a loop.
The way to fix this would probably be something like this
UIS.InputBegan:Connect(function(Input)
if not debounce then
debounce = true
if Input.KeyCode == Enum.KeyCode.LeftControl then
Animate = Humanoid:LoadAnimation(Animation)
Animate:Play()
Humanoid.WalkSpeed = Humanoid.WalkSpeed - 9
Humanoid.JumpPower = 0
else
Animate:Stop()
Humanoid.WalkSpeed = 16
Humanoid.JumpPower = 50
end
debounce = false
end
end)
What is this crouch animation’s priority? If it’s at the Core priority, the walking and standing animation will overlap with the crouching one. You can just move it up one priority, Idle, republish the animation and use the script in the OP.
All of Roblox’s animations run at Core priority by the way.