Hello, I have this script where basically when "Q" is pressed. It'll print something in the output. However, I couldn't seem to make it work. It is a serverscript that is located in ServerScriptService.
local UserInputService = game:GetService("UserInputService") UserInputService.InputBegan:connect(function(keyCode) if keyCode.keyCode == Enum.KeyCode.Q then print("left slot activated") end end)
I've checked my capitalisation as well as spelling. When "Q" is pressed nothing seems to print. There is also no output or any errors or warnings. How would this script be altered for it to work and what is causing it to fail?
UserInputService is a client-only service, which makes sense since the service doesn't give you any way to know which player's input is to be taken, etc.
From the wiki:
Since this service is client-side only, it will only work when used in a LocalScript or a ModuleScript required by a LocalScript. As UserInputService is client-side only, users in the game can only detect their own input - and not the input of others.
The fix is to simply change the script to a LocalScript and place it somewhere in the client's environment, the content of the script can remain the same and will work. Incase you were you using a server script for replication reasons, you'll have to use RemoteEvents to notify the server about input.