How to make a crouch system

hello i want to make a crouch system were if i walk while holding c id play a crouch move animation and when i stay still while still holding c it play a idle crouch like combat warriors or something else like that

i really need help on this becuase im still very terrible at scripting and have no idea how to do this

3 Likes

If you want your game to be compatible with mobile, i recommend you using ContextActionService, else, you can just use UserInputService.

2 Likes

use UserInputService, listen to when C is pressed, then decrease the players walkspeed and either change the default walking/running and idle animation to the ones you want then change it back when the player releases C or track the players velocity, if it’s 0 play the idle animation and if its more than 0 play the running one. when the player releases the C key increase the walkspeed again and stop the animations.

7 Likes

What we could use is UserInputService’s InputBegan/InputEnded Events, and IsKeyDown() function to check for when the C key is held down

The InputBegan & InputEnded Event fires whenever the Player activates/deactivates a certain key, mouse function or whatever else you want it to do, since we wanna detect when the Player is crouching

There are 2 parameters for the event:

  • The Input from that specific player activated
  • If the Event was triggered by a game interface, such as the Chat System

We want to not detect our second parameter, cause that’ll overlap with ROBLOX’s Core UI’s so we’ll have to return (Or end) the function early incase we accidentally trigger it (Or in simple terms, chat while we want to crouch at the same time)

local UIS = game:GetService("UserInputService")

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Animator = Character:WaitForChild("Humanoid"):WaitForChild("Animator")

local IdleID = 0 --Replace this with your Idle Animation ID
local MovingID = 0 --Replace this with your Moving Animation ID

local IdleAnim
local MovingAnim

UIS.InputBegan:Connect(function(Input, Chatted)
    if not Chatted then --Chatted is a bool (True/False)
        return --We want to check
    end

    if Input.KeyCode == Enum.KeyCode.C then -- Check to see if the Key that the player pressed is equal to the Crouch key
        IdleAnim = Animator:LoadAnimation("rbxassetid://"..IdleAnim)
        IdleAnim:Play()
    end
end)

UIS.InputEnded:Connect(function(Input, Chatted)
    if not Chatted then --Chatted is a bool (True/False)
        return --We want to check
    end

    if Input.KeyCode == Enum.KeyCode.C then -- Check to see if the Key that the player pressed is equal to the Crouch key
        IdleAnim:Stop()
    end
end)

This should somewhat give you of a headstart here hopefully :thinking: Another side note, but UIS can only be from the client side, so you’ll have to use a LocalScript

5 Likes

alright ill try this i was doing something else so i didnt realise how many people posted already ill try this out

1 Like
3 Likes