How would I detect what keys a player hits with a server script?

Hello, I am trying to make a script where when a player types a character, the key they typed would be printed out.
Local Script:
ss5

Server Script

Instead of printing out the key that the player typed it prints out the players username instead.

How would I fix this?
All help is appreciated.

1 Like

OnServerEvent implicitly receives the client who fired the remote as the first parameter. Your server is actually receiving three arguments because of this and the third argument is getting dropped because it can’t be assigned to a variable. Remove the LocalPlayer bit from FireServer.

2 Likes

Thanks, it now prints “Enum.KeyCode.W” or something like that, so how would I get it to only print the character and have it remove the Enum.KeyCode part?

W is an EnumItem and it has a name property.

print(Input.KeyCode.Name)
1 Like

Thanks, I didn’t know about that!

Yeah @colbert2677 is correct, the method :FireServer the additional arguments put in as the server automatically identified the player who fired the event through the first parameter of your :Connect(function()) as OnServerEvent works like:

Event:FireServer(parameter_one, parameter_two)

and on the server script

Event.OnServerEvent:Connect(function(Player, paramter_one, parameter_two))

what you have done is

Event:FireServer(Player.name, input.Keycode)
Event.OnServerEvent:Connect(function(player, Input))

where the automatically passed parameter of player has been named to “player” but the second parameter of “player” which you passed has been named to “Input”.

remove the “input” parameter from the server and just use the player parameter

Hey, for some reason whenever I run the script and type the letter O or the letter I, it doesn’t print, but whenever I type any other letter it works.
Here is the script again

local RE = game.ReplicatedStorage.Input
RE.OnServerEvent:Connect(function(Player, Input)
	if #game.Players[Player.Name]:GetChildren() < 4 then
		local Folder = Instance.new("Folder")
		Folder.Parent = game.Players[Player.Name]
		
		local StrValue = Instance.new("StringValue")
		StrValue.Parent = game.Players[Player.Name].Folder
		
	end
	
	if Input.Value >= 97 and Input.Value <= 122 or Input.Value == 32 then
		print(Input.Name)
	end
end)

This is a separate issue. If you’re testing in Studio make sure the “Respect Studio shortcuts when game has focus” option is disabled. Studio observes these keys for the camera and Studio shortcuts have precedence over developer keybinds.

1 Like

Player arguement is passed by default to the server, the reason the username is printed is cause the server sees (player, localPlayer, input) and prints the local player instance which you reference as input, to fix it only pass the keycode to the server by doing RE:FireServer(input.KeyCode).