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

[SOLVED] How do you make something happen when you press a key on your Keyboard?

Asked by 4 years ago
Edited 4 years ago

I've currently been on the discord and have been using "an older type" of script that doesn't work anymore. Is there a newer faster way?

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

Yes, we can use UserInputService. Since it is a form of input, it can only be accessed using a Local Script. It has many functions and uses, but the most common one is InputBegan, It detects the Player's input such as pressed keys in this instance. Due to it being a Local Script, the code inside will not replicate to other clients unless the code is changing Character properties such as speed, I recommend you use a RemoteEvent or RemoteFunction. Scripting a key down script would look something like this.

local UserInputService = game:GetService("UserInputService") -- We are getting the Service.

UserInputService.InputBegan:Connect(function(input, IsTyping) -- Binding the Player's input to an event, in simpler terms, when the player does something such as press a key then this will fire.
    if input.KeyCode == Enum.KeyCode.Q and not IsTyping then -- If the Player presses Q and they aren't typing then run the code, the key does not have to be Q. It can be anything.
        -- Here you would write what you want to do when Q is pressed.
    end
end)
Ad

Answer this question