Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I do a key trigger?

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

Thank you, InfectedRythmCC

2 answers

✓ Accepted answer

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)

3 comments

0

Thank you. It works great :)

0

No problem! Anytime :)

0

It would also work inside StarterPack

local uis = game:GetService("UserInputService")

uis.InputBegan:connect(function(key)

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

print("YouTube Exists")

end

end)

This discussion is preserved as it was. New answers, comments, and votes are closed.