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

Can you detect a key input on a server script?

Asked by 8 years ago

The question says it all. I want to do this because I want a function on the server script to be activated when a key is pressed. How would I make this possible? This is the method that I tried(this was on the server side script)

1function onKeyPress(input)
2    if input.KeyCode == Enum.KeyCode.R then
3        Reload()
4    end
5end
6 
7game:GetService("UserInputService").InputBegan:connect(onKeyPress)

1 answer

Log in to vote
0
Answered by 8 years ago
Edited 8 years ago

Well, UIS won't work on the server, so try using RemoteEvents or RemoteFunctions.

They are pretty similar, the main difference being RFunctions can return values and are accessed as functions instead of events. Example of both:

LocalScript

1local reloadEvent = game.ReplicatedStorage.ReloadEvent -- etc. etc.
2 
3function onKeyPress(input)
4    if input.KeyCode == Enum.KeyCode.R then
5        reloadEvent:FireServer() -- or reloadFunction:InvokeServer()
6    end
7end
8 
9game:GetService("UserInputService").InputBegan:connect(onKeyPress)

ServerScript

01local reloadEvent = game.ReplicatedStorage.ReloadEvent -- etc. etc.
02 
03reloadEvent.OnServerEvent:connect(function(clientFiredFrom)
04    Reload()
05end)
06 
07-- or
08 
09function reloadFunction.OnServerInvoke:connect(clientFiredFrom)
10    Reload()
11end

Hope I helped!

~TDP

Ad

Answer this question