I have a crouch script that makes the player crouch whenever the player presses ‘c’. However, the animation doesn’t stop when I press ‘c’ to uncrouch.
Here is the localscript:
local stateConnections = {}
local crouching = false
local contextActionService = game:GetService("ContextActionService")
local loadedAnim = nil
local debounce = false
local debounceTime = 3
function crouch()
local character = game:GetService("Players").LocalPlayer.Character or game:GetService("Players").LocalPlayer.CharacterAdded:Wait()
if not debounce then
debounce = true
spawn(function() wait(debounceTime); debounce = false end);
if isGame.Value then
local humanoid = character:FindFirstChild("Humanoid")
if humanoid then
if not crouching then
loadedAnim = humanoid:LoadAnimation(script:WaitForChild("Crouching"))
loadedAnim:Play()
loadedAnim.Stopped:Wait()
loadedAnim = humanoid:LoadAnimation(script:WaitForChild("CrouchingIdle"))
loadedAnim:Play()
crouching = true
local connection = humanoid.Running:Connect(function(speed)
if speed > 0 then
-- Player is walking...
else
-- Player is staying still...
loadedAnim:Stop()
loadedAnim = humanoid:LoadAnimation(script:WaitForChild("CrouchingIdle"))
loadedAnim:Play()
end
end)
table.insert(stateConnections, connection)
else
loadedAnim:Stop()
for i, v in pairs(stateConnections) do
v:Disconnect()
end
loadedAnim = humanoid:LoadAnimation(script:WaitForChild("UnCrouching"))
loadedAnim:Play()
crouching = false
end
end
end
end
end
contextActionService:BindAction("Crouch", crouch, true, "c")
This has to be a localscript inside of StarterPlayerScripts.
The issue with it is that the player doesn’t stop crouching when they press ‘c’ again.