How do you use UserInputService

So I was practicing a little bit of scripting in studio, whilst watching Peaspod’s Advanced Tutorial on key pressing events. I scrolled through the comments a little and people have said that the KeyDown and KeyUp function is outdated and that I should be using the UserInputService event instead. So I did a little bit of research on the UserInputService | Documentation - Roblox Creator Hub and I also tried various ways in my script (It was written just to mess around with events and stuff from the object browser), however I still can’t get this to work and I still don’t know how to do this.

--The script I was messing around with just to the the KeyDown and KeyUp events
local tool = script.Parent
local player = game.Players.LocalPlayer
local playerInputService = game.Players.LocalPlayer:GetService()

tool.Equipped:Connect(function()
	playerInputService.UserInputService:connect(function(key)
		if key == "h" then
			print("You a noob")
		else 
			print("You still a noob")
		end
	end)
	
	playerInputService.UserInputService:connect(function(key)
		if key == "h" then
			print("You are the best for letting go of me!")
		else 
			print("Get off of me >:(")
		end
	end)
end)

Here is my code. It is giving me an error: [15:51:05.223 - GetService is not a valid member of Player “Players.HexTheSpy”. Hopefully You guys can help me.
Thank you very much!!! :grinning:

7 Likes

What are you trying to do with Player:GetServer()?

That is not a thing.

Use game:GetService("UserInputService")

5 Likes

First of all you can only get server from game:GetService() and second of all you haven’t defined which service to get so that line should be like this

local playerInputService = game:GetService("UserInputService")
3 Likes

OHHH, that makes so much sense, so I have to get the service first and state what service I want to get right?

Yes that’s what i am talking about you can’t just get the service without including which type of service inside of :GetService()

1 Like

But even if you did do that right it wouldn’t work.

You have to use:

UserInputService.InputBegan:Connect(function(Input)

Instead of:

2 Likes

So this should be the entire script with no function errors:

local tool = script.Parent
local player = game.Players.LocalPlayer
local playerInputService = game:GetService("UserInputService")

tool.Equipped:Connect(function()
	playerInputService.InputBegan:connect(function(input)
		if key == "h" then
			print("You a noob")
		else 
			print("You still a noob")
		end
	end)
	
	playerInputService.InputBegan:connect(function(key)
		if key == "h" then
			print("You are the best for letting go of me!")
		else 
			print("Get off of me >:(")
		end
	end)
end)
3 Likes

Here is a full example code:

local UIS = game:GetService("UserInputService")

UIS.InputBegan:Connect(function(Input)
if Input.KeyCode == Enum.KeyCode.H then

end)
end
2 Likes

That would error:

The parameter is input and the if statement is checking for the parameter ‘key’ which does not exist

2 Likes

And also that’s not how it works you have to use if key.Keycode == "h" then

1 Like

I know that’s why i only said “function errors” but i forgot to change the if key statement

1 Like

alright thanks guys let me ust test this out and see if it works, won’t be a minute!!!

Also why add an secondary InputBegan when it’s the same as the primary one

1 Like

Well im not done correcting the entire code so it you will get a lot of errors in it

1 Like

oh ok, I am watching the topic anyways for new posts

So this code should work with no errors at all

local tool = script.Parent
local player = game.Players.LocalPlayer
local playerInputService = game:GetService("UserInputService")

tool.Equipped:Connect(function()
	playerInputService.InputBegan:connect(function(input)
		if input.KeyCode == "h" then
			print("You a noob")
		else 
			print("You still a noob")
		end
	end)
	
	--Removed the second function since it isn't really necessary at all
end)
3 Likes

Oh wait i almost forgot that the if statement won’t work since it doesn’t work like that so you will need to add input.KeyCode.H In it So it should be like this for the inputbegan function

playerInputService.InputBegan:connect(function(input)
		if input.KeyCode == input.KeyCode.H then
			print("You a noob")
		else 
			print("You still a noob")
		end
	end)
1 Like

There is one problem though, if you unequip the tool, it will still print if you press “h”

I also suggest you use Enums instead of strings:

local tool = script.Parent
local player = game.Players.LocalPlayer
local playerInputService = game:GetService("UserInputService")

local ToolEquipped = false

tool.Equipped:Connect(function()
	ToolEquipped = true
end)

tool.Unequipped:Connect(function()
	ToolEquipped = false
end)

playerInputService.InputBegan:Connect(function(input)
	if ToolEquipped == true then
		if input.KeyCode == Enum.KeyCode.H then
			print("You a noob")
		else 
			print("You still a noob")
		end
	end
end)
4 Likes

Yeah the reason why i had to explain each mistake one by one instead of telling the entire correct script is because i don’t want to get a feedback for sharing scripts

1 Like

That will error, use Enums like I said.

2 Likes