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

Can i have some more help on userInputService?

im having trouble understanding what i can do with it and the differen functions/events

2 answers

✓ Accepted answer

UserInputService is a great replacement for the old technique of getting input from the users keyboard via the PlayerMouse object.


UserInputService is comprised of two kinds of input enumerations:

1: UserInputType - This contains information regarding the PlayerMouse

2: KeyCode - This contains information regarding the keyboard

You can compare these values with what kind of input the user actually gave to the computer. This is done using the equal to operator ( == ) and an if statement, then carrying on with what happens if the statement returns true. Here's an example:

local UIS = game:GetService("UserInputService")

UIS.InputBegan:connect(function(Input)
	local MouseInput = Input.UserInputType -- Get all mouse inputs
	local KeyCode = Input.KeyCode -- Get all keyboard inputs

	-- Now compare them with what matches in the Enum library
	-- If A was pressed, then...
	if KeyCode == Enum.KeyCode.A then
		print("A was pressed")

	-- If B was pressed, then...
	elseif KeyCode == Enum.KeyCode.B then
		print("B was pressed")
	
	-- If the left clicker was clicked (on the mouse) then...
	elseif MouseInput == Enum.UserInputType.MouseButton1 then
		print("Mouse was left clicked")
	end
end)

Here's a list of all the available KeyCodes you can use: http://wiki.roblox.com/index.php?title=API:Enum/KeyCode

Here's a list of all the available UserInputTypes (or, mouse inputs) you can use: http://wiki.roblox.com/index.php?title=API:Enum/UserInputType

I also have a module that makes working with UserInputService a bit easier. You can find it here: https://www.roblox.com/Custom-events-library-Create-or-modify-events-item?id=398814042

If you have any questions about the module, I have a video explaining how it works here: https://www.youtube.com/watch?v=9bggLpCFMOg

Hope that helped, let me know if you have any questions.

1 comment

0

All I can do is give this answer 1000/10 thank you so much

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