Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

If Player Removing fires first when BindToClose happens whats the point of BindToClose?

So In my script when Player Removing fires I save a value and set that value to nil right after

But I don't see a point on BindToClose if BindToClose is going to fire after Player Removing is going to save since it fires first

1 comment

0

BindToClose doesn't fire until the game is set to shutdown. Saving data automatically is good, and doing it twice on rare scenarios like on a last player, is even better.

1 answer

BindToClose can be used in many scenarios. For example, let;s say you want to be informed when a server shuts down. And you want to know how/when it shut down. BindToClose makes this quite easy.

Here's a quick example:

local Players = game:GetService("Players");
local DataStoreService = game:GetService("DataStoreService");

local LogDS = DataStoreService:GetDataStore("Log");

local function gameClosing()
	local Data = nil;
	if (#Players:GetPlayers() > 0) then
		Data = "server shut down for some random reason";
	elseif (#Players:GetPlayers() == 0) then
		Data = "everyone eventually left";
	end;
	LogDS:UpdateAsync(game.JobId,function(old)
		return Data;
	end);
end;

game:BindToClose(gameClosing);

(I have not tested this)

This discussion is preserved as it was. New answers, comments, and votes are closed.