How to use the user input service easily

I will show you a step by step tutorial on how to use the User Input Service as easy as possible through a tool while handling the main portion of things on the server.

The User Input Service is like mouse.KeyDown, except that is deprecated now and the User Input Service is not just for computers unlike mouse.KeyDown.

Now for scripting, first make a tool. If you don’t want a handle in your tool then make sure the “Requires Handle” property in the tool is set to false. Once you have done that make a “Remote Event” and place it inside the tool and name the Remove Event “KeyDown”. Now make a “LocalScript” and place it inside the tool, with the LocalScript put the code as this, everything is explained through the parts where you see – before the sentence.

-- LocalScript

UserInputService = game:GetService("UserInputService") -- this variable gets the user input service so we can use it later

UserInputService.InputBegan:Connect(function(input,event) -- input is for the key
	script.Parent.KeyDown:FireServer(input.KeyCode) -- fires it to the server
end)

Now make a server script and put it into the tool you made, and put the code as this. I explain everything where you see – before a sentence.

-- Script
script.Parent.KeyDown.OnServerEvent:Connect(function(player,key) -- player is from the user who pressed the key and key is the key pressed.
	if player ~= game.Players:GetPlayerFromCharacter(script.Parent.Parent) then return end
	-- the line above this makes it so an exploiter can't activate it and make the script think our player pressed a key.
	
	local letter = key.Name -- this is for letters to make it easier
	letter = letter:lower() -- this turns it into a lowercase letter, for example it turns A into a.
	
	if letter == "a" then -- example of a player pressing a letter on the keyboard
		print("a button pressed")
	end
	-- input.KeyCode is an enum, so getting the name of it would turn for example Enum.KeyCode.A into A, therefore making it easier to use.
	
	-- now if you want a thing that isn't a letter, such as the key "TAB" on the keyboard then do this.
	
	if key == Enum.KeyCode.Tab then -- example
		print("TAB pressed")
	end
	-- you can replace TAB with anything, even letters.
	-- The only reason I made it seperate for letters because it is simply much easier to use that way.
	
end)

Now you can edit it however you’d like, enjoy!

9 Likes