Best way to detect if player is holding down a button?

I have a sprint script that works fine but whenever the player presses another button whilst sprinting (holding shift) they get interrupted and stop sprinting as if the game thinks they arent holding shift anymore.
Any ways to prevent this?
Localscript.

local shift = Enum.KeyCode.LeftShift
local R2 = Enum.KeyCode.ButtonR2

local function HoldDown()
	return UIS:IsKeyDown(shift) or UIS:IsKeyDown(R2)
end

A snippet:

local function Input(input, gameProcessedEvent) -- This is the entire sprint script.
	if HoldDown() then
--rest of sprint 
end
end
UIS.InputBegan:Connect(Input) -- Calls the sprint function.
3 Likes

I’m unsure why’d the character stop printing, but perhaps a better way of listening if the player is holding down Shift or not, is to register when the player first started pressing it, making the player faster accordingly — which you’ve done, though a bit different — and finally listening for when the players stop pressing the same key. As such, in a more general case:

UIS.InputBegan:Connect(function(input, gPE)
    if not GPE then
        if input.KeyCode == Enum.KeyCode.LeftShift then
            -- make player sprint (example)
            localPlayer.Character.Humanoid.WalkSpeed = 20;
        end
    end
end)

UIS.InputEnded:Connect(function(input, gPE)
    if input.KeyCode == Enum.KeyCode.LeftShift then
        -- revert player to normal walking speed
        localPlayer.Character.Humanoid.WalkSpeed = 16;
    end
end)
1 Like

Player still stops sprinting whenever they press another button. (Such as 1 or D)

Here is the entire script

local stamina = 15
UIS.InputBegan:Connect(function(input, GPE)
	if not GPE then
		if input.KeyCode == Enum.KeyCode.LeftShift then
		if Humanoid.MoveDirection.Magnitude > 0 then
		if not (character.Humanoid.WalkSpeed <= 7) then
			character.Humanoid.WalkSpeed = NewWalkSpeed
			sprinting = true
			while stamina > 0 and sprinting do
						stamina = stamina - .03
						Bar.Size = UDim2.new(stamina / 10, 0, 1, 0)
						Num.Text = stamina
				--Bar.BackgroundColor3 = Bar.BackgroundColor3:lerp(Color3.fromRGB(255, 42, 42), 0.001)
				tween:Play()
				task.wait()
						if stamina <= 0 then
					tween2:Play()
					runninganim:Stop()
					sprinting = false
					character.Humanoid.WalkSpeed = NormalWalkSpeed
				end
				end
				end
		end
	end
	if  character.Humanoid.WalkSpeed == 0 and runninganim.IsPlaying then
		runninganim:Stop()
	end
	end
end)
1 Like

Are you sure that just pressing a different key is in fact the culprit? The issue doesn’t seem very obvious, and it does not look to be an issue with how inputs are being registered.

Yes i am very much sure, i tried equipping a tool (WITH NO SCRIPTS) and it stops sprinting, it also even regenerates the “stamina” faster, while pressing two directional keys (W and D) decreases stamina faster. Also this is how i set the sprinting boolvalue to true.

Humanoid.Running:Connect(function()
	if Humanoid.MoveDirection.Magnitude > 0 then
		if sprinting == true then
			runninganim:Play()
		end
	else
			runninganim:Stop()
	end
end)

What I did for my sprinting system is I created a variable to check whether the player is sprinting and change the variable when the player clicks the key-bind for sprinting.

Sample script below to make it more easier to understand.

local Sprinting = false
UIS.InputBegan:Connect(function(input, GPE)
       if not GPE then
              if input.KeyCode == Enum.KeyCode.LeftShift then
                     if Sprinting == false then
                            Sprinting = true
                            Player.Character.Humanoid.WalkSpeed = 25
                     end
              end
       end
end)
UIS.InputEnded:Connect(function(input, GPE)
       if input.KeyCode == Enum.KeyCode.LeftShift then
              if Sprinting == true then
                      Sprinting = false
                      Player.Character.Humanoid.WalkSpeed = 16
              end
       end
end)

Not sure if this would make a difference or fix the problem, but feel free to criticize.

2 Likes