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

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

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

1 comment

0

Humanoid.Walkspeed* and ur changing it to the same value

1 answer

✓ Accepted answer

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!

2 comments

0

Tell me if there are errors and I will get back to you ASAP

0

Thanks, it worked flawless.

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