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)
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.
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 );
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.
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!
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
(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.
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.
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.