How do I use Players.PlayerRemoving?

Hello everyone.
How do I use Players.PlayerRemoving to fire a remote event. In my game Clash of Roblox, i’m trying to detect when a player bails from a game that I have, so that the player that didn’t rage quit doesn’t get stuck in the battlefield, my code in the local script is:

player = game.Players.LocalPlayer
local Gui = player.PlayerGui --Cant remember what i did here LOL, supposed to reference the players gui

Players.PlayerRemoving:Connect(function(player)
   if Gui.ScreenGui.GameGuiRed.Visible == true then
      game.ReplicatedStorage.RemoteEvents.RedBail:FireServer("RedBail")
   end
end)

I can’t get the event to fire when the player, is it because the player take the script down with them too? Should I put that function in a server script?

Put it in a server script.

But LocalPlayer will don’t work

Edit: wait he have LocalPlayer in a block sorry

I’d not recommend using . Instead use the functions :WaitForChild() or :FindFirstChild()

PlayerRemoving works on the client but the above responses are correct in that you should be performing this action from the server. Leaving the client authoritative over declaring their bail can lead to variable problems either with the client not firing the remote or an exploiter intentionally blocking the function from running (especially if bailing players are intended to be penalised).

I would recommend looking into your structure instead of trying to force this code to work because that way you can fix underlying problems as well. The server should mark who’s on what team so that it can perform certain actions such as this case of determining when a player leaves the match midgame.

3 Likes

Script in ServerScriptService:

game:GetService("Players").PlayerRemoving:Connect(function(p)
    local Gui = p.PlayerGui -- then complete it
    if Gui.ScreenGui.GameGuiRed.Visible == true then
        -- u don't need to use remove event here
    end
end)

You’re firing a remote event to the server from a server script… that won’t work.

Either use FireClient or do it on the server (depending on what is needed to be done).

1 Like

Wait then what i need to do…?

You could just put the code inside the script you fired within the script located in ServerScriptService (the one we just told you to put there), and transfer it to the script in ServerScriptService. You can’t fire the server from the server, just like you can’t fire the client from the client. It’s client to server, and server to client replication.

2 Likes
game.Players.PlayerRemoving:Connect(function(player)
    local gui = player.PlayerGui
    if gui.ScreenGui.GameGuiRed.Visible == true then
        -- code here
    end
end)

Right then, i’ll place the code in the server.

PS its 2:48 am where I am lol

So, what are you doing in the .OnClientEvent??

Hello everyone, thank you all so much for the help, placing the script in the server has proven to work. I placed the solution on the person that gave the most detailed correct answer. Though consider that ya’ll got the :white_check_mark: Solution
Resume: Detect a player leaving the game, use a server script in server script service.

PS: Don’t forget to sub to Osrock626 on YT, helps out quite a bit.

1 Like