Problems with UserInputService

Hello Developers,

I am trying to use UserInputService and it wasn’t working. So, I tried to make a simple script with it for testing and it doesn’t work. Is there something wrong with it? Am I using it wrong?

Script

local UserInputService = game:GetService("UserInputService") 

UserInputService.InputBegan:Connect(function(input) 
	if input.KeyCode == Enum.KeyCode.W then
		print("W")
	elseif input.KeyCode == Enum.KeyCode.A then
		print("A")
	elseif input.KeyCode == Enum.KeyCode.S then
		print("S")
	elseif input.KeyCode == Enum.KeyCode.D then
		print("D")
	end
end)

A few questions.

  1. What script type is it?
  2. Where is it located?
  3. Are you sure it is not disabled?

I tested it in a local script within starter character scripts, it fully works and prints on input began.

Edit: Please refer to the dev forumn API referencing

There is a big red header that says

NotReplicated
This item is not replicated across Roblox’s server/client boundary.

This means it only works on a client so yes a starter gui would work, refer to here for places where the local script would work.

  1. Regular Script.
  2. ServerScriptService
  3. Yes.

Then of course it doesn’t work. The server has no keyboard, it can’t handle client-sided user input.

Make it a LocalScript in PlayerScripts.

Alright. So would this script work in StarterGui to open a gui?

Yes, you just have to replace the “print()” with “script.Parent[“yourguiname”].Frame.Visible = true/false
Just add a Frame inside your GUI.

local UserInputService = game:GetService("UserInputService") 

UserInputService.InputBegan:Connect(function(input) 
	if input.KeyCode == Enum.KeyCode.W then
		script.Parent.Gui.Frame.Visible = true
	elseif input.KeyCode == Enum.KeyCode.A then
		script.Parent.Gui.Frame.Visible = false
	elseif input.KeyCode == Enum.KeyCode.S then
		print("S")
	elseif input.KeyCode == Enum.KeyCode.D then
		print("D")
	end
end)

Explorer Screenshot:
https://gyazo.com/8ea46ce19fd4e6bc63f1bff47419da24