Binding To Close not working properly

Currently I want to warn my players a shutdown is incoming.

My code is

game:BindToClose(function()
	ClosingGame = true
	--if game:GetService("RunService"):IsStudio() then return end
	MessageService:PublishAsync("ServerDeleted", {game.JobId})
	for _,pl in ipairs(game.Players:GetPlayers()) do
		wait(0.5)
		local playerFolder = game.ServerStorage.PlayerData:FindFirstChild(pl.Name.."_Data")
		if not playerFolder then return end
		
		playerFolder.Parent = nil
		
		local joind = pl.TimeJoined.Value
		playerFolder.LastPlayed.Value = os.time()
		playerFolder.TimePlayed.Value = playerFolder.TimePlayed.Value + playerFolder.SessionTime.Value
		
		saveData(pl,playerFolder)
	end
	
	local msg = Instance.new("Message")
	msg.Text = "Server is shutting down in 15 seconds..."
	msg.Parent = workspace
	task.wait(15)
	print("Shutting Down")
end)

It is neither waiting the 15 seconds nor creating the message. It is simply saving player data.
Anyone has a solution for this?

Have you tried removing the wait in that loop and then testing?

Apparently something on saveData was yielding. Thanks anyway…

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