Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 5 years ago

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

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. SmartNode 383 — 5y

1 answer

Log in to vote
0
Answered by
0b1w 13
5 years ago
Edited 5 years ago

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)

Ad

Answer this question