How to make C to crouch and C to unCrouch? [Toggle, Toggling]

  1. What do you want to achieve?
    I want to make a script that makes your character crouch when you press C and unCrouch when you press C again, instead of the hold C to crouch.
  2. What is the issue?
    I don’t know what to search for to learn about the problem and its solution.
  3. What solutions have you tried so far?
    As I am a noob at scripting I tried counting the C presses using a variable and implementing them in the code [if inputObject.KeyCode == Enum.KeyCode.C and cCount==0 then] . But as the script loops around, the variable gets initialized every time which brings me back to square one.

This is what the code looks like.

UserInputService.InputBegan:Connect(function(inputObject, processed)
	if processed then return end 
	if inputObject.KeyCode == Enum.KeyCode.C  then
		crouchServerEvent:FireServer(player, Character)
	end
end)
UserInputService.InputBegan:Connect(function(inputObject, processed)
	if processed then return end 
	if inputObject.KeyCode == Enum.KeyCode.C  then
		unCrouchServerEvent:FireServer(player, Character)
	end
end)
2 Likes

Make a debounce to identify if its on crouching or uncrouching phase.

--like this
local isCrouching = false
UserInputService.InputBegan:Connect(function(inputObject, processed)
	if processed then return end 
	if inputObject.KeyCode == Enum.KeyCode.C then
        if isCrouching == false then
		    crouchServerEvent:FireServer(player, Character)
            isCrouching = true
        elseif isCrouching == true then
		    unCrouchServerEvent:FireServer(player, Character)
            isCrouching = false
        end
	end
end)
4 Likes

You really wouldn’t need to fire a server event. All crouching does is play/stop an animation and change your HipHeight, both which are replicated on client and server from LocalScripts.

Here’s a video similar to what you are trying to achieve: Making Crouch/Crawl Button - Teddy Horror Gamedev #6 - YouTube

1 Like
function CrouchFunc(_, inputState)
    if inputState == Enum.UserInputState.Begin then
        crouchServerEvent:FireServer(player, Character)
    else
        unCrouchServerEvent:FireServer(player, Character)
    end
end

ContextActionService:BindAction("CrouchToggler", CrouchFunc, false, "c")


Ideally, like @SheriffTaco said, you don’t need a FireServer at all.

1 Like

Ideally I would use animations for crouch… Ideally that is. Reality is my dumbass used a script from toolbox that uses tweenmodule to make the the crouch and also prone. I didn’t stop to question why a crouch script would be almost 400 lines long. And then I made it server-sided…so all the damage is done :'). Thanks a bunch for the suggestions!
edit1: I forgot the word toggle exists
edit2: There is a surprising lack of resources on toggling inputs.

This script doesn’t toggle crouch however, it acts as hold crouch :o as it uncrouches after I let go . @Sorbius 's one does the toggle perfectly.

My bad, I forgot to set a bool to say whether crouch was being toggled or not.

1 Like

I am interested though, could you say how and where you would set the bool?
Edit: nevermind lol, apparently I didn’t take a good look at Sorbius’s solution.