This is an IMPOSSIBLE bug.
To keep it short, there’s some incredibly strange behaviour, causing me to go 2 nights without good sleep. I’ve done some tests, you can easily repro these:
local Players = game:GetService("Players")
Players.PlayerRemoving:Connect(function (player)
print("Bye", player)
end)
game:BindToClose(function()
print(#Players:GetPlayers())
end)
The above will execute the code bound to server shutdown using BindToClose prior to Player.PlayerRemoving.
This suggests that the player is removed after the function bound fires and the print is executed. However, this does NOT seem to be the case, since 0
is printed, instead of 1
- in a solo Studio test session.
local Players = game:GetService("Players")
game:BindToClose(function()
print(#Players:GetPlayers())
end)
The above usually works as expected, and prints 0
. On multiple occasions, it had printed 1
.
local Players = game:GetService("Players")
Players.PlayerRemoving:Connect(function (player)
wait(1)
end)
game:BindToClose(function()
print(#Players:GetPlayers())
end)
The above acts exactly the same as the second code snippet I have posted in this post. It has printed 1
, once. Unlike the first snippet, which has always printed 0
.
This issue has stumped many a scripter. I have had a bad dream yesterday about this. I woke up feeling angry. I am still angry. This is an extremely hard problem to solve. Please, guidance would be appreciated.