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

How to detect left shift key up?

Asked by
hellmatic 1523 Moderation Voter
6 years ago

I am trying to create a sprint script and when you press shift it fires the function. I am stuck on how to detect when shift is not being pressed. This is what I have so far

function onSprintKeyPress(inputObject, gameProcessed)
    if SprintSettings.CanSprint and not SprintSettings.IsSprinting and inputObject.KeyCode == SprintSettings.Key then 
        Sprint()
    end
end

UIS.InputBegan:connect(onSprintKeyPress)
1
; awesomeipod 607 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

An inputObject includes a changed event. You could use this to detect when the key is released or you could use the InputEnded and use a boolean to check its state.

Using the changed event

function onSprintKeyPress(inputObject, gameProcessed)
    if SprintSettings.CanSprint and not SprintSettings.IsSprinting and inputObject.KeyCode == SprintSettings.Key then 
        Sprint() -- enable sprint
    inputObject.Changed:Wait() -- only the input state can change with a key

    -- disable sprint
    end
end

UIS.InputBegan:Connect(onSprintKeyPress)

Using the InputEnded event

local function onSprintKeyPress(inputObject, gameProcessed)
    if SprintSettings.CanSprint and not SprintSettings.IsSprinting and inputObject.KeyCode == SprintSettings.Key then 
        Sprint() -- enable sprint
    end
end

UIS.InputBegan:connect(onSprintKeyPress)

local function onSprintRelease(inputObject, gameProcessed)
    if inputObject.KeyCode == SprintSettings.Key and SprintSettings.IsSprinting then
    -- disable sprint
    end
end
UIS.InputEnded:Connect(onSprintRelease)

I hope this helps. Please comment if you do not understand how / why this code works.

Ad

Answer this question