Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why isn't this ServerScript that detects everytime the key "Q" is pressed not working?

Asked by
ka3r 14
4 years ago

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?

1 answer

Log in to vote
0
Answered by 4 years ago

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.

0
P.S. Use :Connect() over :connect(), always avoid using deprecated methods! ankurbohra 681 — 4y
0
Thanks! May you explain what a "deprecated method" is and how I could avoid them? I'm guessing they are outdated or abandoned methods? Explain if you have time (I'm a new player to studio). ka3r 14 — 4y
0
You got it, deprecated methods are methods that have been left in favour to other methods. In Roblox, these are usually just changed concerning cases for uniformity but, deprecated methods might (or at all) not be updated as opposed to their counterparts. It's good practice to avoid them! ankurbohra 681 — 4y
Ad

Answer this question