Maintaining Win Streak on BindToClose()

I have created a game which has 1v1, 2v2, etc. matches. Players have win streaks to show off their skills to friends. I want the win streak to reset if a player leaves mid-round, so they cannot avoid losing the streak just by giving up entirely. However, when the game shuts down, I do not want the player to lose their streak, as this is obviously out of their control.

This code block assigns the second parameter (true) to the session data under the key “ActiveDuel” which is how I store whether a player should lose their streak upon leaving. This value is set to false at the end of a round, and ideally when the server shuts down.

local profile = PlayerProfileService:GetProfile(player)

if profile ~= nil then
	profile:SetSessionData("ActiveDuel", true, false)
end

My initial attempt was to set this value to false whenever BindToClose fires. The following code is ran when a server shuts down.

for _, profile in ipairs(PlayerProfileService:GetProfiles()) do
	profile:SetSessionData("ActiveDuel", false, false)
end

I tested this with a friend, and it still resets our win streaks. This means that the PlayerRemoving event is firing before BindToClose. I am unsure how to fix this, so any advice would be very helpful.

Have a lastplayer variable to store the final player to leave the game before the server shuts down because :BindToClose doesn’t have a player reference. This will be useful later. In your PlayerRemoving function, check if the number of players in the game is 1 (event fires before the player leaves). If there is 1 player only then set the lastplayer to the that player with lastplayer = player (Define it outside the function so the bindtoclose can access it too)
Then in your :BindToClose you can modify that player’s profile to do what you want

Shutdown resets the streak of every player in the server, not just the last to disconnect.

By the time that server is shutting down there will no players left right?

The problem I have is the order in which the events fire. I need to know that the server is shutting down before I determine whether the streak is reset or not. However, the shutdown code runs after all players already have their streaks wiped.

You should probably manage the streak everytime the player leaves instead of when the server shuts down. You could have an attribute of the player called ‘IsInRound’ and if they are leaving while that’s true then reset their streak

Yes, this is exactly what the system does lol

Then why do you need that server shut down anymore

I don’t think you’re following what my problem is, so hopefully someone else has some ideas

Why not reset the win streak after the match, not when the player leaves? That way, BindToClose can forcefully end the game and determine if it is ended via shutdown.

That or set their win streak back to their previously stored win streak on BindToClose.