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

How do I make a KeyDown script with UserInputService?

Asked by
funyun 958 Moderation Voter
8 years ago

I'm not quite used to UserInputService, I've only been using the Mouse object. Can someone show me how to do something like this with UserInputService? Sorry if it sounds like a request, but I'm lost.

player = game.Players.LocalPlayer
mouse = player:GetMouse()

mouse.KeyDown:connect(function(key)
    if key == "f" then
        print("sup")
    end
end

1 answer

Log in to vote
3
Answered by 8 years ago

Due to KeyDown becoming deprecated, using UserInputService is the only way to get the press of a key. In order to use UserInputService, you must first use :GetService() and from there you can use the InputBegan event so that you can tell when the person gives any form of input. From here you can just use a simple function with parameters to figure out which key they pressed. You will need the KeyCode List in order to find the KeyCodes of keys that you don't know.

Please note that this is not a complete script but is merely the function that you would use to find a pressed key.

game:GetService("UserInputService").InputBegan:connect(function (input, _) -- recognizes input is being given
    if input.KeyCode == enum.KeyCode.W then -- checks to see if the key pressed was the W key
        print("the player pressed W")
    end
end)
0
In line 2 it should be Enum.KeyCode.W not enum.KeyCode.W UserOnly20Characters 890 — 8y
0
Sorry my bad FearMeIAmLag 1161 — 8y
Ad

Answer this question