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?
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.
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 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.
game.Players.PlayerRemoving:Connect(function(player)
local gui = player.PlayerGui
if gui.ScreenGui.GameGuiRed.Visible == true then
-- code here
end
end)
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 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.