Problems with shift to sprint script

Im making a script for a shift to sprint feature but for some reason it doesnt work. No errors.
Script:

local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
local useri = game:GetService("UserInputService")
local pressing

mouse.KeyDown:Connect(function(key)
	if key == Enum.KeyCode.LeftShift then
		pressing = true
		while pressing == true and wait(0.1) do 
			plr.Character.Humanoid.WalkSpeed = plr.Character.Humanoid.WalkSpeed + 20
		end
	end
end)

mouse.KeyUp:Connect(function(key)
	if key == Enum.KeyCode.LeftShift then
		pressing = false
		plr.Character.Humanoid.WalkSpeed = plr.Character.Humanoid.Walkspeed - 20
	end
end)

You should use useri.InputBegan and useri.InputEnded instead.

1 Like

I have never heard of KeyDown and KeyUp in mouse, it doesn’t even says in the Developer Hub. As @Qroz says, you may use UserInputService. You can use ContextActionService also, this will let you add a mobile button for touch device users.

1 Like

But how do I detecct is it is the shift key in the if statement using imputbegan?

Like @Qroz said, you should be using UserInputService or ContextActionService events cause Mouse events are deprecated. –
For your code, you mentioned that you are using a “Script”, which is wrong cause you are doing LocalPlayer and that is a client-only object. I will also add that I would add a WalkSpeed limit cause there isn’t a limit in your code.

local max = 100;
plr.Character.Humanoid.WalkSpeed =  math.min(plr.Character.Humanoid.WalkSpeed + 20, max );
1 Like

I was using a local script, I just said script because idk.

UserInputService.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.LeftShift then
--Code
end
end)
1 Like

I don’t have the answer, but I would recommend not using shift, as for some reason, there’s glitches from some keyboards (including my own) where I type using Shift+7 and then the sprint would turn on, as my keyboard doesn’t have a / button and i use shift+7 to make a dash.
Maybe CTRL? or something else, idk.

1 Like

I’d also like to note the conflict with Shift-MouseLock (if you plan to keep that feature.) The problem is that
if you want to keep the MouseLock feature on the right shift, it won’t work for the Windows Store / UWP ver.
of the client, as it sends Enum.Keycode.LeftShift for a right shift!

1 Like

Well first of all you should use the control key instead of shift as shift lock uses the same keybind and this might be anoying to some players.

Second i see a a big problem with the script that it has a loop which always adds 20 to the speed giving players infinite speed which would not revert back to the original. You would fix this by removing the loop while pressing == true and wait(0.1) do

Also you should not use a while wait( loop see here for more info: Avoiding wait() and why

1 Like

I dont think my game needs shift lock, it could even be bad for the gameplay, is there any way of disabling it?

(This is not my script and was taken from this guy called ISontaran)
These are pictures taken from a script which works perfectly, sorry if the quality is unclear, my PC isn’t the best.

Yes there is see : StarterPlayer | Documentation - Roblox Creator Hub

I would not suggest to disabling shift lock though as many players use it and it will hurt game play. Personally it is anoying to me when games disable it.

There is a mechanism to change the keys used for it. It’s kindof complicated: You have to get the
MouseLockController of the CameraModule and as a StringValue child called BoundKeys
you can have your own comma-separated list of key names (last part of the enum.)
Or you can simply do nothing and be fine. :slight_smile:

This will be a wargame with weapons which will have a custom locked mouse and boats which will have cannons that you control with the mouse. Players will be using those features most of the time, so I dont think it would hurt the gameplay.

Used this script:

local player = game.Players.LocalPlayer
 
while true do
	player.DevEnableMouseLock = player.DevEnableMouseLock
	wait(5)
end

And got this error:
image

Yes. You should disable the .EnableMouseLockOption in game>StarterPlayer

1 Like

For clarification: A checkbox in Studio.

1 Like