Variables not updating in game:BindToClose()


Currently, I’m experimenting with server shutdown scripts and I ran into an issue where when I update a variable in game:BindToClose() it doesn’t update for the PlayerRemoving event. My goal for this script is to make a PlayerRemoving event detect when the server is shutting down. Is there another possible method if my method is wrong or cannot be fixed?


-- Services
local PlayersService = game:GetService("Players")

-- Variables
local ShuttingDown = false

-- Events
	if ShuttingDown then return end
	print(plr.Name, "joined the server!", "Shutting Down:",ShuttingDown)

	if ShuttingDown then return end
	print(plr.Name,"left the server.", "Shutting Down:",ShuttingDown)

	ShuttingDown = true
	print("Set ShuttingDown to", ShuttingDown)

Console Output:

  12:13:22.531  StormyZach2007 joined the server! Shutting Down: false  -  Server - Datastore Shutdown Handling:10
  12:13:25.996  Set ShuttingDown to true  -  Server - Datastore Shutdown Handling:20
  12:13:25.977  Disconnect from ::ffff:|59416  -  Studio
  12:13:25.979  StormyZach2007 left the server. Shutting Down: false  -  Server

maybe because whenever player lefts server gets removed

Well the thing that confuses me is that before the server exiled the player, it said the variable was updated. Although the code that’s responsible for the player leaving says otherwise.

maybe because isshuttingdown gets changed after everyone left?

game:BindToClose() invokes when a game’s server is shutting down. A cool behaviour within this is that, the server doesn’t actually shutdown immediately but rather yields for any other operations to finish. The maximum duration of this brief period would be 20 seconds.

Also the reason why this is happening is because Player.PlayerRemoving() happens immediately and shuts down the “server (not actually a server but your local machine)”. when you’re playing testing in studio. Notice how the time between the message “Set ShuttingDown to True” and “StormyZach2007 has left the server” happened? This happens because your computer is actually hosting a local session when you’re solo play testing (this is why there is no chat filter when you’re in roblox studio).

PlayerRemoving is invoking before Game:BindToClose() which subsequently doesn’t allow for your check to happen.

This issue should solve itself when you’re playing normally within the roblox game client.

1 Like

Thank you so much! Now I understand it pretty well

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.