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 5 years ago
Edited by royaltoe 5 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.

01local UIS = game:GetService('UserInputService')
02local Player = game.Players.LocalPlayer
03local Character = Player.Character
04 
05PlayAnim = nil
06 
07UIS.InputBegan:connect(function(input, gameProcessedEvent)
08    if input.KeyCode == Enum.KeyCode.W then
09        Character.Humanoid.WalkSpeed = 16
10        local Anim = Instance.new('Animation')
11        Anim.AnimationId = 'rbxassetid://04690454085'
12        PlayAnim = Character.Humanoid:LoadAnimation(Anim)
13        PlayAnim:Play()
14 
15    elseif input.KeyCode == Enum.KeyCode.C then
View all 24 lines...

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 — 5y

2 answers

Log in to vote
0
Answered by
royaltoe 5144 Moderation Voter Community Moderator
5 years ago
Edited 5 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.

01local UIS = game:GetService('UserInputService')
02local Player = game.Players.LocalPlayer
03local Character = Player.Character
04 
05local isCrouching = false
06isOnCooldown = false
07idleCrouchAnimation = nil
08sneakAnimation = nil
09 
10--checks if the player is running (wasd keys + shift key)
11function isRunning()
12    if
13        UIS:IsKeyDown(Enum.KeyCode.LeftShift) and UIS:IsKeyDown(Enum.KeyCode.LeftShift)
14        or UIS:IsKeyDown(Enum.KeyCode.W) and UIS:IsKeyDown(Enum.KeyCode.LeftShift)
15        or UIS:IsKeyDown(Enum.KeyCode.A) and UIS:IsKeyDown(Enum.KeyCode.LeftShift)
View all 87 lines...
Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 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.

01local Crouching = false
02function Crouch()
03    if Crouching == false then
04        Crouching = true
05        -- Crouch anim
06    else
07        Crouching = false
08        -- Crouch anim
09    end
10end
0
Do I put this inside of the crouching script or the sneak script? I dont understand completely. DukeOswald 7 — 5y

Answer this question