For a project I'm doing, I need to make a sprint script that increases your walkspeed and widens your camera angle a little bit when you hold down shift. (For future reference, this is a LocalScript.) No problem--I've made one before. The issue arose when I felt like making a brand new one just because.
Since I was a little rusty on my scripting, I popped onto ROBLOX Wiki and looked up the instance Mouse
to find the keybinding events associated with it, but ROBLOX Wiki did not list any keybinding events under Mouse
as usual. In the absence of these, I attempted to look up KeyDown
, one such keybinding event. At the top of the resulting page, there was a message with a background of red stating that KeyDown is deprecated and exists only for compatibility reasons, and is not to be used in new scripts. I went to the pages of related events (such as KeyUp
) and found the same message.
This stumped me a little. I cannot seem to find any new method of keybinding, if there even is one. How do I keybind now that the old events are deprecated?
Using Dyler3's idea, I'm going to make it way less complicated for you.
local uis = game:GetService("UserInputService") --The service local plyr = game:GetService("Players").LocalPlayer uis.InputBegan:connect(function(Key, Processed) --Processed is so you don't sprint when you press Shift in chat. if Key.KeyCode == Enum.KeyCode and not Processed then plyr.Character.Humanoid.WalkSpeed = 32 end end)
This is the simplified version of Dyler's script. The problem with this script is... ONCE YOU PRESS SHIFT YOU WILL STAY IN SPRINT FOREVER! To prevent this problem use InputEnded.
local uis = game:GetService("UserInputService") --The service local plyr = game:GetService("Players").LocalPlayer uis.InputEnded:connect(function(Key, Processed) if Key.KeyCode == Enum.KeyCode and not Processed then plyr.Character.Humanoid.WalkSpeed = 16 end end)
Now mix it all together:
local uis = game:GetService("UserInputService") --The service local plyr = game:GetService("Players").LocalPlayer uis.InputBegan:connect(function(Key, Processed) if Key.KeyCode == Enum.KeyCode.LeftShift and not Processed then plyr.Character.Humanoid.WalkSpeed = 32 end end) uis.InputEnded:connect(function(Key, Processed) if Key.KeyCode == Enum.KeyCode.LeftShift and not Processed then plyr.Character.Humanoid.WalkSpeed = 16 end end)
Hope it helps!
The only other way that I'm aware of being able to do this, is by using the UserInputService. This can pretty much track any interaction that the user makes with the game. Here's an example of a script that I've made (LocalScript inside PlayerGui):
Plr=game.Players.LocalPlayer Mouse=Plr:GetMouse() UIS=game:GetService("UserInputService") UIS.InputBegan:connect(function(Type) if Type.UserInputType==Enum.UserInputType.Keyboard then local Keys=UIS:GetKeysPressed() local AllKeys="" for i,Key in pairs(Keys) do local StringKey=tostring(Key.KeyCode) AllKeys=AllKeys..StringKey:sub(14):lower() if i~=#Keys then AllKeys=AllKeys..", " end end print(AllKeys) end end)
Basically, this is just a more complex method of doing the same thing as KeyDown().
Anyways, hope this helped clear things up for you a bit. If you have any further questions, please leave a comment below. Hope I helped :P