Getting local player from a server script

I want to make a keypress that when I press E it CFrames a model above my shoulder, what I want to know is how I make a keypress detect the local player and bring a model to the local players character.
Any help would be greatly appreciated! Thank you.

You can create a local script that makes use of User Input Service or Context Action Service and whenever a player presses E, fire a remote event. When the server gets the request, CFrame the model to the player’s character.

Edit:
For the server to know which player pressed E, the player object is always passed as a parameter whenever the server gets the request.

RemoteEvent.OnServerEvent:Connect(function(Player, Data)
    local Character = Player.Character
end)
2 Likes

It’d be RemoteEvent.OnServerEvent:Connect.

3 Likes

Use a LocalScript that detects input, send the local player to the server with a remote event.

On client.

game:GetService("UserInputService").InputBegan:Connect(function(input, proc)
 if input.KeyCode == Enum.KeyCode.E and proc then
  remoteevent:FireServer(params)
 end
end)

On server.

remoteevent.OnServerEvent:Connect(function(player, params)
 local char = player.Character
 if char then
  --CODE
 end
end)

Edit: No need to send the local player, remote events already have the player who fired them as first parameter.

The server cannot fetch a LocalPlayer. You can instead use the first argument of a RemoteEvent which passes the player who called the remote. In this case, well, the “LocalPlayer”. The same is also applicable to a RemoteFunction.

local function yourFunction(player, ...)
    print(...)
end

RemoteEvent.OnServerEvent:Connect(yourFunctiom)
RemoteFunction.OnServerInvoke = yourFunction
-- Note: You must have a return value for a function defined for a RemoteFunction, otherwise the calling thread will permanently hang

As for key presses, I’m sure you’ve heard of the UserInputService (or ContextActionService).