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

OHH, i missed one thing.

Lemme show you the finished one.

Finished:

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

local IsCrouching = false

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

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 = not IsCrouching
			
			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: Added the characteradded wait

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.