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

How do I get it to run code when the player presses a key? IsKeyDown/KeyCode

TL:DR How do I get it to run code when the player presses a key?

What I'm trying to do right now is figure out how key detection works. I'm trying to figure out how to make it so when I press a key it runs a part of code/an entire script. Example: if (game:GetServices('UserInputService:IsKeyDown') == true) then print("boi") end This bit of code gives me an error that says "GetServices is not a valid member of DataModel."

1 comment

0

because iyou need to remove the s it is game:GetService

1 answer

GetServices() isn't a method, that is why you're getting an error. Use GetService() and use UserInputService like this:

local UIS = game:GetService("UserInputService") -- Like this...
UIS.InputBegan:Connect(function(input, GPE)
	if input.UserInputType = Enum.UserInputType.Keyboard then
		local key = input.KeyCode
		print("boi, I pressed " .. key)
		if GPE then
			print("the game engine observed my key press, yo!") -- Just a bit of fun here :P
		end
	end
end)

To understand what I'm doing here, check out InputBegan.

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