Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

ROBLOX: How do I Keybind Without Using Deprecated Events?

Asked by
Minifig77 190
9 years ago

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?

0
You can still use them, they'll work perfectly fine. dyler3 1510 — 9y
0
I know, I just want to use the most up-to-date way to do it. Minifig77 190 — 9y
0
I don't think there is a more up-to-date way. The only other way would be to use UserInputService. I could post an answer explaining that a bit if you want? dyler3 1510 — 9y
0
Sure. Minifig77 190 — 9y

2 answers

Log in to vote
1
Answered by 9 years ago

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)

Final Product

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!

0
Shouldn't it be "Enum.KeyCode.LeftShift", rather than "Enum.KeyCode"? Redbullusa 1580 — 9y
Ad
Log in to vote
0
Answered by
dyler3 1510 Moderation Voter
9 years ago

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

-Dyler3

0
Wouldn't it be easier if you just used KeyCode? "Type.KeyCode == Enum.KeyCode.LeftShift" This will apply to what the asker wants and you don't have to deal with lines 8-16. "print(Type.KeyCode)" Redbullusa 1580 — 9y

Answer this question