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

“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

The code shown by @Sarchyx is not vulnerable to any particular problems. Exploiters are able to tell the server that they pressed a key, but that should not be a vulnerability in and of itself.

1 Like

It works, however the code is running twice, since it prints twice.

If the person can press the key itself, and the same would happen than using an exploit just to press a key, then it isn’t vulnerable

ContextActionService fires the callback function multiple times during different steps of the input. You have to differentiate between press and release as detailed in the article (see the bottom code example where they check the input state for Begin).

I see, but I’m sure it’s fine since It’s a bool value and it only changes its value once.

You’d be correct that in this case it firing twice is no problem. However, for code correctness, it’s best to make sure you’re only firing the event on key press instead of release.

1 Like

Well, it fires twice since it starts and it ends, if you only want it to fire once do this:

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