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)
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.
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)
@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.