Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to detect left shift key up?

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 comment

1

;

1 answer

✓ Accepted answer

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.