Due to a combat logging system, I need to know when players are being kicked due to a server restart; and if they are, ignore the fact they’re in combat to not incorrectly punish them for combat logging.
Old threads suggest BindToClose, but it fires when the server is effectively closing, after all players have been removed; or at least that’s what my tests have implied:
game.Players.PlayerRemoving:Connect(function(player)
print("PlayerRemoving fired")
end)
game:BindToClose(function()
print("BindToClose fired. Getting players in-game:")
for _, player in ipairs(game.Players:GetPlayers()) do
print(player.Name)
end
end)
I tried both, manually leaving (which causes the server to close once my Player instance is gone) and forcefully stopping the local server using the Stop button. Are my results possibly wrong and BindToClose actually works anyway for server restarts?
As far as im aware, BindToClose fires before player instances are removed from the DataModel. It should be acceptable to grab a list of players in the game, as you do in your code example, and exempt them from any combat logging system you have implemented.
If you’re stopping your client in studio than it’s likely it is disconnecting your client before the server automatically shuts down since it detects 0 players.
This still wasn’t solved. BindToClose only fires when it’s shutting down, and when restarting, the server only shuts down after all players left the game. The players who left also do not have a specific leave reason, as the only enums for PlayerExitReason are PlatformKick and CreatorKick.
I’m assuming there’s no ways to do this other than setting up a global announcement to remove combat tag from players in all servers and then restart after triggering this announcement.