Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by
Nump4d 5
3 years ago

Please provide more explanation in your question. If you explain exactly what you are trying to accomplish, it will be much easier to answer your question correctly.
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

Log in to vote
0
Answered by 3 years ago

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.

Ad
Log in to vote
0
Answered by
Pupppy44 671 Moderation Voter
3 years ago

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

0
this script works without any errors, but when I hold down the key it doesnt stay in the crouching position Nump4d 5 — 3y
0
Then there might be a problem with the animation, check if it loops. radiant_Light203 1166 — 3y

Answer this question