Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How would I make this hold down key crouch script work?

local player = game.Players.LocalPlayer
local humanoid = player.Character.Humanoid
local UserInputService = game:GetService("UserInputService")
local anim = Instance.new("Animation")
anim.AnimationId = "5351326151"

local LeftControl = Enum.KeyCode.LeftControl

local function LeftControlDown()
	 if UserInputService:IsKeyDown(LeftControl) then
		local PlayAnim = humanoid:LoadAnimation(anim)
		PlayAnim:Play()
	end 
end

Basically I am trying to replicate the Minecraft sneak mechanic where when you hold down a key it plays the crouching animation, and when you release it then you return to a stand animation. Any help would be greatly appreciated.

2 answers

You would need to detect when the player begins pressing down the left control key by using UserInputService.KeyDown and that would make it begin to crouch. Then make sure the crouching animation is a looping animation. Then make it stop playing the crouching animation by using UserInputService.KeyReleased.

Just use the InputBegan event. Example:

local player = game.Players.LocalPlayer
local humanoid = player.Character.Humanoid
local UserInputService = game:GetService("UserInputService")
local anim = Instance.new("Animation")
anim.AnimationId = "5351326151"

local LeftControl = Enum.KeyCode.LeftControl

local function LeftControlDown(k)
     if k.KeyCode == LeftControl then -- If the key pressed is LeftControl
        local PlayAnim = humanoid:LoadAnimation(anim)
        PlayAnim:Play()
    end 
end

UserInputService.InputBegan:Connect(LeftControlDown) -- The event for InputBegan

Hope this helps

2 comments

0

this script works without any errors, but when I hold down the key it doesnt stay in the crouching position

0

Then there might be a problem with the animation, check if it loops.

This discussion is preserved as it was. New answers, comments, and votes are closed.