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

How do I do a key trigger? [closed]

Asked by 5 years ago

So in case you're wondering what I mean: like if a press the key "H" it would print Hello World.

Thank you, InfectedRythmCC

Closed as Not Constructive by hiimgoodpack, Le_Teapots, Psudar, zblox164, SirDerpyHerp, and User#5423

This question has been closed because it is not constructive to others or the asker. Most commonly, questions that are requests with no attempt from the asker to solve their problem will fall into this category.

Why was this question closed?

2 answers

Log in to vote
2
Answered by 5 years ago
Edited 5 years ago

I suggest you reading the following articles from the wiki: https://developer.roblox.com/en-us/api-reference/event/UserInputService/InputBegan https://developer.roblox.com/en-us/api-reference/enum/KeyCode

For your example, the code would be like this: (Writen in a local script inside StarterPlayerScripts)

local UserInputService = game:GetService("UserInputService")
local hotkey = "H"
local message = "Hello world"

function OnInputBegan(input, gameProcessed)
    -- Check if triggered by the keyboard
    if input.UserInputType == Enum.UserInputType.Keyboard then
        local keyPressed = input.KeyCode
        -- Check if the key pressed was 'hotkey'
        if keyPressed == Enum.KeyCode[hotkey] then
            print(message)
        end
    end
end

UserInputService.InputBegan:Connect(OnInputBegan)
0
Thank you. It works great :) SamZeKat 28 — 5y
0
No problem! Anytime :) Le_Teapots 913 — 5y
0
It would also work inside StarterPack develop_d 53 — 5y
Ad
Log in to vote
0
Answered by 5 years ago
local uis = game:GetService("UserInputService")

uis.InputBegan:connect(function(key)

if key.KeyCode == Enum.Keycode.H then

print("YouTube Exists")

end

end)