What is wrong with my code? | UserInputService

So i have this line of code. But it doesn’t run.
The code is sampled from Alvin Blox because my code didn’t work.
Here is the code:

local UIS = game:GetService(“UserInputService”)
UIS.InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.Keyboard then
if input.KeyCode == Enum.KeyCode.W then
print(“w”)
end
end
end)

Leave any feed back!
This Script is stored in ServerScriptService.

Here is your problem, you are not able to detect keypresses from the client on the server. To fix this problem you need to move this script into a local script.

1 Like

Oh, Thanks for the tip and now i am one step closer to becoming a better Codder.
Thanks alot mate!

2 Likes
local UIS = game:GetService("UserInputService")

UIS.InputBegan:Connect(function(input, processed)
	if processed then
		return
	end
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if input.KeyCode == Enum.KeyCode.W then
			print("W")
		end
	end
end)

Remember to use “processed” to determine if keypresses occurred because the player interacted with one of the core user interfaces (chat for example) so that the callback function can be ignored (returned).

instead of returning end, why dont you add “and not processed” to the if statement?
makes it look nicer, but it’s all up to personal preference i assume.

Because a single check is quicker than two.

local UIS = game:GetService("UserInputService")

UIS.InputBegan:Connect(function(input, processed)
	if not processed then
		if input.UserInputType == Enum.UserInputType.Keyboard then
			if input.KeyCode == Enum.KeyCode.W then
				print("W")
			end
		end
	end
end)

This would be similar to what you’re referring to however.

i’ve had experiences with returning end where it completely passes trough when it’s repeated fast, and yeah that also looks nicer, a single if statement at the top is better than returning end in my opinion.

function a()
	function b()
		return --returns b not a
	end
end