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

How do I tell if a game has shut down or not?

Im using a bool to tell if a game has shut down or not

So lets say a boolvalue in workspace

if game shuts down it turns into true

that way I can execute other scripts depending on if it shutdown or not how do i do this?

1 answer

✓ Accepted answer

If I am correct, roblox provides this function BindToClose() Which allows you to run any function before the game gets shutdown. Your can read more about it on the developer page (Link:DataModel:BindToClose), but I will also tell what it does. So when you configure server shutdown via game's page, this function detects that the game is about to be shutdown from this line of code:

game:BindToClose() --"This function detects if the game is about to shutdown. 

By using this function, you can do things like for example, if you want to change the boolvalue you can do this:

game:BindToClose(function()
	script.Parent.Value = true
end)

However, instead of using bool value I think you can directly connect to function like I did it below.

local function doStuffBeforeShutdown()
	--Do anything you need to do before the game shuts down. For example, I will make a message to tell everyone that game is shutting down.
	local msg = Instance.new("Message",workspace) --I am just using Message object because it is easy to use. However, you should try to make one from text label because this object is deprecated.
	msg.Text = "The game is about to shutdown!"
end

game:BindToClose(doStuffBeforeShutdown)

One thing you have to keep in mind is that when this function is called, it only gives you maximum of 30 seconds before the game closes, so think carefully when you add script to make it run within time limit.

I hope my explanation is not confusing, because I was in rush.

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