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.

You are watching: How to crouch in roblox

What is the issue?I don’t know what to search for to learn about the problem and its solution.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 . 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 thencrouchServerEvent:FireServer(player, Character)endend)UserInputService.InputBegan:Connect(function(inputObject, processed)if processed then return end if inputObject.KeyCode == Enum.KeyCode.C thenunCrouchServerEvent:FireServer(player, Character)endend)
Make a debounce to identify if its on crouching or uncrouching phase.

--like thislocal isCrouching = falseUserInputService.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 endendend)
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: https://youtu.be/yGdQC-A5gVk


function CrouchFunc(_, inputState) if inputState == Enum.UserInputState.Begin then crouchServerEvent:FireServer(player, Character) else unCrouchServerEvent:FireServer(player, Character) endendContextActionService:BindAction("CrouchToggler", CrouchFunc, false, "c")Ideally, like
SheriffTaco said, you don’t need a FireServer at all.

See more: Mike Sweeney For Congress - Candidate Profile Mike Sweeney


1 Like
tonyredgraveX (tonyredgraveX) January 17, 2021, 12:46pm #5

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 existsedit2: 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 .