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

Sneak Only While Crouching?

Asked by 4 years ago
Edited by royaltoe 4 years ago

Im trying to make it so that when I crouch I can sneak. The problem is that when I stand the same code applied makes me sneak even while standing.

local UIS = game:GetService('UserInputService')
local Player = game.Players.LocalPlayer
local Character = Player.Character

PlayAnim = nil

UIS.InputBegan:connect(function(input, gameProcessedEvent)
    if input.KeyCode == Enum.KeyCode.W then 
        Character.Humanoid.WalkSpeed = 16
        local Anim = Instance.new('Animation')
        Anim.AnimationId = 'rbxassetid://04690454085'
        PlayAnim = Character.Humanoid:LoadAnimation(Anim)
        PlayAnim:Play()

    elseif input.KeyCode == Enum.KeyCode.C then 

    end
end)

UIS.InputEnded:connect(function(input, gameProcessedEvent)
    if input.KeyCode == Enum.KeyCode.W then
        PlayAnim:Stop()
    end
end)

The key to crouch is "C" so how do I say "When you press C that it will sneak and when you press C again then sneak is disabled"

???

0
make a local variable called Crouching and set it to false (local Crouching = false) and then when the player crouches you check if Crouching is true, if not then set it to true and crouch, otherwise set it to false and uncrouch. joshmatt2244 28 — 4y

2 answers

Log in to vote
0
Answered by
royaltoe 5144 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

We ended up solving it on discord. Took a while but we got it done. Here is the final result:

If you are reading this and have any question, post them below pls and I'll get back to u.

local UIS = game:GetService('UserInputService')
local Player = game.Players.LocalPlayer
local Character = Player.Character

local isCrouching = false
isOnCooldown = false
idleCrouchAnimation = nil
sneakAnimation = nil

--checks if the player is running (wasd keys + shift key)
function isRunning()
    if 
        UIS:IsKeyDown(Enum.KeyCode.LeftShift) and UIS:IsKeyDown(Enum.KeyCode.LeftShift)
        or UIS:IsKeyDown(Enum.KeyCode.W) and UIS:IsKeyDown(Enum.KeyCode.LeftShift)
        or UIS:IsKeyDown(Enum.KeyCode.A) and UIS:IsKeyDown(Enum.KeyCode.LeftShift)
        or UIS:IsKeyDown(Enum.KeyCode.S) and UIS:IsKeyDown(Enum.KeyCode.LeftShift)
        or UIS:IsKeyDown(Enum.KeyCode.D) and UIS:IsKeyDown(Enum.KeyCode.LeftShift)
    then
        return true 
    end
end

UIS.InputBegan:Connect(function(input, gameProcessedEvent)
    --if the player presses the crouch key, and theyre not on cooldown:
    if input.KeyCode == Enum.KeyCode.C and not isOnCooldown then
        isOnCooldown = true

        --if theyre running play the slide animation
        if isRunning() then
            --SLIDE ANIMATION 
        else 
            --if theyre not crouched, play crouch animation
            if not isCrouching then
                isCrouching = true
                Character.Humanoid.WalkSpeed = 6

                local animation = Instance.new('Animation')
                animation.AnimationId = 'rbxassetid://04690414995'
                idleCrouchAnimation = Character.Humanoid:LoadAnimation(animation)

                idleCrouchAnimation:Play()

            --if they are crouched, stop crouch animation
            else 
                isCrouching = false
                idleCrouchAnimation:Stop()
                Character.Humanoid.WalkSpeed = 16
            end
        end


        --wait .5 seconds and remove cooldown
        wait(.5)
        isOnCooldown = false

    --if theyre crouched and pressing w, play sneak animation
    elseif input.KeyCode == Enum.KeyCode.W and isCrouching then
        local animation = Instance.new('Animation')
        animation.AnimationId = 'rbxassetid://04690454085'
        sneakAnimation = Character.Humanoid:LoadAnimation(animation)
        sneakAnimation:Play()
    end
end)



--stop crouch idle when the player is running
UIS.InputBegan:Connect(function(input, gameProcessedEvent)
    print(isRunning(), sneakAnimation)
    if isRunning() and sneakAnimation  then
        sneakAnimation:Stop()
            sneakAnimation = nil
    end

    if (input.KeyCode == Enum.KeyCode.LeftShift and UIS:IsKeyDown(Enum.KeyCode.W)) 
    or (input.KeyCode == Enum.KeyCode.W and UIS:IsKeyDown(Enum.KeyCode.LeftShift)) then
        idleCrouchAnimation:Stop()
    end
end)

--stop sneak animation when the player stops pressing w while sneaking
UIS.InputEnded:Connect(function(input, gameProcessedEvent)
        if input.KeyCode == Enum.KeyCode.W and sneakAnimation then
            sneakAnimation:Stop()
            sneakAnimation = nil
    end
end)

Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

Make a local variable called Crouching and set it to false (local Crouching = false) and then when the player crouches you check if Crouching is true, if not then set it to true and crouch, otherwise set it to false and uncrouch.

local Crouching = false
function Crouch()
    if Crouching == false then
        Crouching = true
        -- Crouch anim
    else
        Crouching = false
        -- Crouch anim
    end
end
0
Do I put this inside of the crouching script or the sneak script? I dont understand completely. DukeOswald 7 — 4y

Answer this question