How to make crouch and crawl in the same key

Hi developers, I wanted to know how do games make a crouch and crawl system on the same key, for example, in The game Recoil you can prone and crouch with C

1 Like

When the crouch/prone key is pressed, wait for a bit and if the key is still pressed, make them prone. If the key, then make them go prone.

1 Like

You could use a bool, which will become true if someone presses c once to crouch. and if they press it again and its still true then you can go onto prone. If they press it again and its still set to true then set the bool to false and position them back up. There is definitely a large scale of methods you can do. This is what I thought of in my head.

You can use UserInputService or ContextActionService for this.

1 Like

Hypothetical code:

function onCKeyPressed()
	if crouch.IsPlaying then
		crouch:Stop()
		prone:Play()
	elseif prone.IsPlaying then
		prone:Stop()
	else
		crouch:Play()
	end
end

game:GetService("UserInputService").InputBegan:Connect(function(key, chatting)
	if not chatting and key.KeyCode == Enum.KeyCode.C then
		onCKeyPressed()
	end
end)
1 Like

Yeah that’s totally possible, just put the code in a localscript inside startercharacterscripts and reference your animation instances where it says:

local char = script.Parent
local human = char:WaitForChild("Humanoid")
local crouchAnim = --reference crouch anim
local proneAnim = --reference prone anim
local crouch = human:LoadAnimation(crouchAnim)
local prone = human:LoadAnimation(proneAnim)
function onCKeyPressed()
	if crouch.IsPlaying then
		crouch:Stop()
		prone:Play()
	elseif prone.IsPlaying then
		prone:Stop()
	else
		crouch:Play()
	end
end

game:GetService("UserInputService").InputBegan:Connect(function(key, chatting)
	if not chatting and key.KeyCode == Enum.KeyCode.C then
		onCKeyPressed()
	end
end)

also, ya mind marking my reply as the solution hehe :relaxed:

1 Like

Here is the script it doesn’t work when I Press C

Screenshot

I was pressing C and It didn’t work.
https://gyazo.com/19f1bf1d8f4b917003c2aa44fdd3fe19

image

when i said to reference animations, i meant to actually make animation objects in the explorer and reference them. E.g. script.CrouchAnim

I don’t understand can u give me an example

image
Make one of these, call it CrouchAnim and parent it to the script, go into properties,
image
paste your animation’s ID inside the “AnimationId” property and reference in the script like I said:

local crouchAnim = human:LoadAnimation(script.CrouchAnim)

do I replace ‘local crouch = human:LoadAnimation(crouchAnim)’ with that script? or create a separate line

replace ‘local crouch = human:LoadAnimation(crouchAnim)’

image

image
the first two variables should be deleted.
You should have renamed the second two (the first to crouch and the second to prone).
image
this bit isn’t needed.

i think hes new

local char = script.Parent
local human = char:WaitForChild("Humanoid")
local crouchAnim = script:WaitForChild("Crouch")
	local proneAnim = script:WaitForChild("Prone")
	local crouch = human:LoadAnimation(crouchAnim)
local prone = human:LoadAnimation(proneAnim)
function onCKeyPressed()
	if crouch.IsPlaying then
		crouch:Stop()
		prone:Play()
	elseif prone.IsPlaying then
		prone:Stop()
	else
		crouch:Play()
	end
end

game:GetService("UserInputService").InputBegan:Connect(function(key, chatting)
	if not chatting and key.KeyCode == Enum.KeyCode.C then
		onCKeyPressed()
	end
end)

add 2 animation under the localscript 1 named Crouch and another named Prone