How can I make a player crouch when they click "C" on pc or "B" on Xbox?

How can I make a player play a crouch animation when they click “C” on pc or “B” on Xbox, Using UserInputService

You would want to make a function using the UserInputService.

do you want player to hold the button to crouch or press once

Just Press once then once they press the key again they stop playing the animation, I know how to do most of it I just don’t know how to play/stop the animation.

try this maked using openai chatgpt

local player = game.Players.LocalPlayer
local character = player.Character
local humanoid = character:WaitForChild("Humanoid")

-- Bind the crouching functionality to the "C" key on PC or the "B" button on Xbox
local crouchKey = Enum.KeyCode.C
local crouchButton = Enum.KeyCode.ButtonB

-- Create a variable to track whether the player is crouching or not
local isCrouching = false

-- Add an animation to the crouching state
local crouchAnimation = Instance.new("Animation")
crouchAnimation.AnimationId = "rbxassetid://1234567890" -- Replace this with the ID of your crouching animation

-- Create a variable to store a reference to the playing animation
local playingAnimation = nil

game:GetService("UserInputService").InputBegan:Connect(function(inputObject, gameProcessedEvent)
    if inputObject.KeyCode == crouchKey or inputObject.KeyCode == crouchButton then
        if not isCrouching then
            -- Crouch and play the crouching animation
            humanoid:ChangeState(Enum.HumanoidStateType.Crouching)
            playingAnimation = humanoid:LoadAnimation(crouchAnimation)
            playingAnimation:Play()
            isCrouching = true
        else
            -- Stand up and stop the crouching animation
            humanoid:ChangeState(Enum.HumanoidStateType.Standing)
            playingAnimation:Stop()
            playingAnimation = nil
            isCrouching = false
        end
    end
end)

This script will bind the crouching and standing up functionality to the “C” key on PC or the “B” button on Xbox. When the player presses either of these keys or buttons, the player’s character will crouch and play the crouching animation if they are not already crouching. If the player is already crouching, the character will stand up and stop the crouching animation.

I did try this, each time i got this error: 5.895 Unable to cast value to Object - Client - Crouch:36
07:41:45.895 Stack Begin - Studio
07:41:45.895 Script ‘Players.MasonX890.PlayerScripts.Player.Crouch’, Line 36 - function onButtonPress

The AI was unable to fix the error

lemme try to fix it with ai one sec

God… People are being infected by the goofy ah ChatGPT…

This is for scripting help dude. Script it yourself and help the person out if you’re a true scripter.

4 Likes

The (Enum.HumanoidStateType.Crouching) is comming up as a error in the function humanoid:ChangeState(Enum.HumanoidStateType.Crouching)

That is not a state, that’s the error.

Lemme whip up a simple script and help ya out, hang in there.

That’s because it’s not a valid HumanoidStateType

1 Like

Enum.HumanoidStateType.Crouching isn’t a state, you’re going to need a custom function to manage crouching.
Enum.HumanoidStateType.Standing isn’t a state, either, but since the crouching state doesn’t exist you don’t need either :ChangeState() line.

Heyo!

So this is the completed script, lemme know for any errors.

local PCInput = Enum.KeyCode.C
local ConsoleInput = Enum.KeyCode.ButtonB

local IsCrouching = false

local Character = game.Players.LocalPlayer.Character

local AnimationID = "rbxassetid://" -- paste your id for animation here
local Animation  = Instance.new("Animation")
Animation.Parent = Character
Animation.AnimationId = AnimationID
Animation.Name = "CrouchAnimation"

local LoadedAnimation = Character:WaitForChild("Humanoid"):LoadAnimation(Animation)

local UIS = game:GetService("UserInputService") -- User input service for detecting input

UIS.InputBegan:Connect(function(input, gpe)
	
	if input.UserInputType == Enum.UserInputType.Keyboard then
		
		if input.KeyCode == PCInput then
			
			IsCrouching = true
			
			if IsCrouching then
				
				LoadedAnimation:Play()
				
			elseif IsCrouching == false then
				
				LoadedAnimation:Stop()
				
			end
			
		end
		
	elseif input.UserInputType == Enum.UserInputType.Gamepad1 then
		
		if input.KeyCode == ConsoleInput then
			
			IsCrouching = not IsCrouching

			if IsCrouching then

				LoadedAnimation:Play()

			elseif IsCrouching == false then

				LoadedAnimation:Stop()

			end
			
		end		
		
	end
	
end)

EDIT: @MasonX890 Make sure your animation is looped!

Here is the only error I encountered: Players.MasonX890.PlayerScripts.Player.Crouch:14: attempt to index nil with ‘WaitForChild’

I put this in StarterPlayerScripts

Mkae sure your script is a local script.

It is, could it be because I put it into a folder?

You could put the script in StarterGui, I may have missed something.

This line should be:

local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()

The script can run before the player’s character loads and that would make it nil in the script. In this revised code, this gets the player’s character if it did load, or waits for it to load (The event returns the player’s character)

Or, if this is in StarterCharacterScripts, you can just do

local Character = script.Parent

Ah yes, my bad. Thank you for pointing that out.

I did script it in a hurry.

2 Likes

It works, But when i click C it doesn’t stop playing the animation