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

Why is this fuction not picking up on left shift being held down?

Asked by 5 years ago

I'm trying to make a sprint button with the shift button. When I hold down the left shift the message will not appear nor will the speed of my character change. What needs to be fixed?

(Ran in a local script)

local UserInputService = game:GetService("UserInputService") 
local isPressed = UserInputService:IsKeyDown(Enum.KeyCode.LeftShift)
if isPressed then
    repeat
    game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = RunningSpeed.Value
    until not isPressed
    game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = WalkingSpeed.Value
end
0
Humanoid.Walkspeed* and ur changing it to the same value MaxDev_BE 55 — 5y

1 answer

Log in to vote
1
Answered by 5 years ago

Try:

local UserInputService = game:GetService("UserInputService") 
local isPressed = false
local plr = game.Players.LocalPlayer
local Char = plr.Character or plr.CharacterAdded:Wait()

UserInputService.InputBegan:Connect(function(Input, GameStuff)
    if GameStuff then return end
    if Input.KeyCode == Enum.KeyCode.LeftShift then
        isPressed = true
        while UserInputService:IsKeyDown(Enum.KeyCode.LeftShift) and isKeyPressed do
            wait()
            Char.Humanoid.WalkSpeed = RunningSpeed.Value
        end
        isPressed = false
        Char.Humanoid.WalkSpeed = WalkingSpeed.Value
    end
end)

UserInputService.InputEnded:Connect(function(Input)
    if Input.KeyCode == Enum.KeyCode.LeftShift then
        isPressed = false
    end
end)

This might fix it

Hopefully, this helped.

Best Of Luck Developer!

0
Tell me if there are errors and I will get back to you ASAP BlackOrange3343 2676 — 5y
0
Thanks, it worked flawless. ninja_eaglegamer 61 — 5y
Ad

Answer this question