How do I tell the server script, listening for the RemoteEvent, the player's name?

This topic has been solved, thanks everyone

I’m making a story sort of game and there’s a leave button for the player to get off the vehicle. When they click the leave button from their side, it fires 2 RemoteEvents that are responsible for teleporting them to spawn and allowing them to move (walk and jump) again.

The problem is, I am not sure how to tell the script that listens for the firing of the event the username of the player.

I have tried using the Roblox wiki’s RemoteEvents article, other DevForum posts, and Google. I’ve not been able to find answers. This DevForum post is my last resort

Local script:

local button = script.Parent

local player = game.Players.LocalPlayer

local EM = game.ReplicatedStorage.EnableMovement
local TTS = game.ReplicatedStorage.TeleportToSpawn

button.MouseButton1Click:Connect(function()
	button.Visible = false
	print("Buttons gone")
	EM:FireServer()
	TTS:FireServer()
	print("Events fired")
end)

Server script:

local spawnTeleport = game.Workspace.SpawnTeleport

local EM = game.ReplicatedStorage.EnableMovement
local TTS = game.ReplicatedStorage.TeleportToSpawn

-- local player = ?
local character = player.Character
local humanoid = character:FindFirstChild("Humanoid")
local humanoidRootPart = character.HumanoidRootPart

TTS.OnServerEvent:Connect(function()
	humanoid.Jump = true
	humanoidRootPart.Position = spawnTeleport.Position
	print("Player teleported")
end)

EM.OnServerEvent:Connect(function()
	humanoid.UseJumpPower = true
	humanoid.JumpPower = 0
	print("Player can move")
end)

A remote event’s OnServerEvent function passes in the player as their first argument
Ex

RemoteEvent.OnServerEvent:Connect(function(playerwhofired)
    print(playerwhofired.Name)
end)

Yeah, but how would the server script read that and know who to take action on?

The serverscript will respond directly to the player who fired the remote event and the player argument passed in will always be the player who called the RemoteEvent:FireServer()

And if two people call the same RemoteEvent:FireServer(), you don’t really need to worry about that since the server can treat them as two seperate events

So, if the local script has code that prints the player who fired the event’s name, then the server script will be able to pick that up and know who to move to the lobby?

The local script doesnt need to pass any argument in the :FireServer() for the server to know who fired. It’s just a default argument in any OnServerEvent.

1 Like

Oh, I didn’t know that. I thought you needed to TELL the server who fired the event for it to work

When you use OnServerEvent, the first argument inside the function is player, so when you fire a server it automatically gets instance of the player inside game.Players. So: OnServerEvent:Connect(function(player)

What do I do with the variable “player”? Does it refer to the character or the actual player?

-- local character = player.Character

local humanoid = character:FindFirstChild("Humanoid")

local humanoidRootPart = character.HumanoidRootPart

TTS.OnServerEvent:Connect(function(player)

humanoid.Jump = true

humanoidRootPart.Position = spawnTeleport.Position

print("Player teleported")

end)

EM.OnServerEvent:Connect(function(player)

humanoid.UseJumpPower = true

humanoid.JumpPower = 0

print("Player can move")

end)
1 Like

It refers to the player, if you want to access the character you can do Player.Character

So, player.Character:WaitForChild(“Humanoid”) etc

Please mark as solved if it works.

@Decablocks is right here. The first argument in receiving an event on the server-side is always the player that sent it, so there’s no need to look for the player.
If you were to send from a server to client instead, then you need to specify the player you’re sending it to in the first argument.

The variable “player” is the actual player instance inside of game.Players. To get the character, you can do player.Character. to get the player’s username you can do player.Name.

It worked, I don’t have a lot of experience with RemoteEvents and functions with the variable inside the (). Thanks