How do I turn a key press event into a server sided one?

I’m not exactly sure how to, I know it has something to do with Remotes but I really, really don’t know how to. I’m trying to make it so that when a player presses a key, it turns a BoolValue from false to true, but I’m not exactly sure how to, is there some sort of server sided key press event thing or do I actually have to use remotes?

You actually have to use remotes, but be careful. This maybe can be exploited in some way, like faking inputs (even tho they can still do it without remotes, it’s just easier).

And well, this is how remote events work. In the client script, its the easiest part,

event:FireServer(key);

server:

event.OnClientEvent:Connect(function(client, key)
    print(client.Name .. "pressed the key:" key);
end);
1 Like

I’m still not exactly sure how to do it, I’m still beginner level for scripting so I still have absolutely no idea how to use remote events correctly.

1 Like

Remote Events might look very hard in the start, but they are very simple to understand.
They act as a gate between the server and the client, imagine a bank for example. The bank has many clients, you can tell the bank something, but only the bank will know since its the one that manages everything, and the bank can also tell you something.
Remote events are the connection between the bank and the clients in this example.

So first create the remote event in “ReplicatedStorage”.

So I believe you know what LocalScripts and ServerScripts are, if no then it’s fine. Local Scripts are code run in YOUR computer, that means that anything you put in that code, the computer can see it but the server cant. Server scripts are code run in the server (only the server can see it), so the server can communicate with all the players, but the player cant communicate alone to all the other players.

So in your local script, get the remote event:

local UserInputService = game:GetService("UserInputService");
local remoteEvent = game:GetService("ReplicatedStorage"):WaitForChild(put the name of the remote even here);

UserInputService.InputBegan:Connect(function(input)
	remoteEvent:FireServer(input.KeyCode);
end);

in the server script, use this:

event.OnClientEvent:Connect(function(client, key)
    print(client.Name .. "pressed the key:" key);
end);

Ah, I think I’m starting to see now, but where do I put the key I want to have to change the value? And what do I do with the event part in the event.OnClientEvent:Connect(function(client, key), since it’s an error.

Can I ask why you want to fire a remote event every time the player presses a key?
This can cause issues with lag on the server when remote events are fired too rapidly, although it depends on what you’re doing on the server.

1 Like

“event” is the same event in Rep storage. So that means:

game:GetService("ReplicatedStorage"):WaitForChild(name of the remote here)

Think of it like a wave starter, I press a button, and a wave starts.

the key can be analyzed in the Server

event.OnClientEvent:Connect(function(client, key)
    print(client.Name .. "pressed the key:" key);

    if(key == Enum.KeyCode.P) then
        print("The key P was pressed");
    end;
end);

Enum.KeyCode has all the inputs from all the devices allowed from roblox

Hi!!! I use remote events all the time and maybe I can help. Remote events are used to transfer info from client to server, visa versa. Fires make a tool or something and add a local script. Find the local player and then Userinpiutservice, then find the key being pressed etc.

Then fire the remote event event:FireServer(the variable for your key pressed stuff)
then to recieve that key create a script and type out a variable for the remote event in replicated storage then right out this function:

function onevent(key variable thing you transmitted via remote event)
-- your code
end

(your event variable).OnServerEvent:Connect(onevent)

I hope this helped, it probably didnt because I did a really bad job at explaining it.

Don’t send keypress events to the server. Instead, handle them on the client, and fire the appropriate action to the server as a RemoteEvent.

Bad:

UserInputService.InputBegan:Connect(function(input)
    keypressed:FireServer(input.KeyCode)
end)

Good:

contextActionService:BindAction('ReloadWeapon', function()
    reload:FireServer()
end, true, Enum.KeyCode.R)

The server isn’t getting told “you pressed R”. It’s getting told “please reload my weapon” - sure, the user tells the client to do that by pressing R, but that’s not relevant to the server itself.

Additionally, by using ContextActionService, you get mobile touchscreen buttons for free.

For additional advice on how to use events in your game, visit this article on the Developer Hub: Bindable Events and Functions | Roblox Creator Documentation

1 Like

OnClientEvent is used for server-client, in this case you’re going from client-server so you should use OnServerEvent instead. KeyCode has its own Enum library, so you can check if the key which was pressed is any of that library.

PD: It’s better to check this on the client so it cannot be exploited and can also have to do with performance.

-- Client
local UIS = game:GetService("UserInputService")
local RS = game:GetService("ReplicatedStorage")

local Key = "F" -- your key

UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode[Key] then
		RS.RemoteEvent:FireServer()
	end
end)
-- Server
local RS = game:GetService("ReplicatedStorage")
local BoolValue = RS.BoolValue -- your boolvalue

RS.RemoteEvent.OnServerEvent:Connect(function()
	BoolValue.Value = true
end)
1 Like

Generally, this is terrible advice because exploits take control of the client.

1 Like

Yes, @Unarthadoxx if your case is a single key do this (for the client):

local remoteEvent = game:GetService("ReplicatedStorage"):WaitForChild(put the name of the remote even here);
local ContextActionService = game:GetService("ContextActionService");
ContextActionService:BindAction("PressedWaveButton", function()
	remoteEvent:FireServer()
end, true, Enum.KeyCode. key code here);

and in the server do this:

local remoteEvent = game:GetService("ReplicatedStorage"):WaitForChild(put the name of the remote even here);
remoteEvent.OnClientEvent:Connect(function(client)
    print(client.Name .. "pressed the key for the wave button");
end);
1 Like

They can take control of RemoteEvents fired from the client, not LocalScripts directly. Think execution as a new LocalScript which can fire RemoteEvents to the server, if you pass crucial information through parameters it can be easily overwrited and exploited to take advantage.

As someone who’s used script executors before, not only can they run new scripts, they can tamper with objects used by existing scripts, hijack execution of those scripts, patch running script bytecode and much, much more. Do not rely on some functionality being out of reach of someone’s executor. If it’s done on the client, it can be controlled.

2 Likes

They can take control of everything

1 Like

OnClientEvent can only be used on the client, is an error I’m getting. @Sarchyx’s code works for what I’m trying to do, however y’all are saying it’s very vulnerable to exploits?

sorry, OnServerEvent. I switched them up. so replace OnClientEvent with OnServerEvent

1 Like