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
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.
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.
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)
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
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
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
Make one of these, call it CrouchAnim and parent it to the script, go into properties,
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)â
the first two variables should be deleted.
You should have renamed the second two (the first to crouch and the second to prone).
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