Crouch script not working

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.

1 Like

Maybe try this? Here is the script’s source video.

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local Animate 
local Humanoid = player.Character:FindFirstChild('Humanoid')

mouse.KeyDown:Connect(function(Key) 
 if Key == "c" then
  local Animation = Instance.new("Animation", player.Character)
  Animation.AnimationId = "rbxassetid://YourID"
  Animate = Humanoid:LoadAnimation(Animation)
  Animate:Play()
 end  
end)

mouse.KeyUp:Connect(function(Key)
 if Key == "c" then
  Animate:Stop()
 end
end)
2 Likes

This solution uses the Mouse object (via Player:GetMouse()). I think the person wants to use ContextActionService, hence he isnt using Mouse.
That being said, your solution would work if he were to use the Mouse object, or even UserInputService for that matter. One thing to point out though is that he wants to toggle crouching on the key press, hence you can press and let go of c, you start / stop crouching. In your example, you stop crouching as soon as you let go of c.

3 Likes

Any functions related to Keys in the mouse object is Deprecated. (which makes sense because its called mouse and not keyboard)

1 Like