How do you make sure that when you shutdown a game, any player in combat gets taken out of it before they’re removed from the game?
This is dependent on how your combat logging system is setup. It could however be as simple as deactivating the system before shutting down but this would likely have to be an ingame command etc.
If you wanted to deactivate it remotely you would have to figure out a way to detect when a server shutdown has been activated and turn off the logging system accordingly but as to my knowledge there isn’t really a way to detect a game shutdown (someone correct me if Im wrong plz)
How would I deactivate the system for every server though? What if there’s like 80+ servers?
Hello!
You can use this event
game:BindToClose(function())
-- Every code writed here will be fired when the server is shutting down
end)
There are two options
- Use MessagingService, it’s a really useful API for sending messages across servers.
- Use DataModel:BindToClose(). This allows you to bind a function to run when the server gets shutdown.
Example:
--This is the Wiki example, however their example shows how to use it to save data, everything inside the function will be run before the server shuts down
game:BindToClose(function()
-- if the current session is studio, do nothing
if RunService:IsStudio() then
return
end
print("saving player data")
-- go through all players, saving their data
local players = Players:GetPlayers()
for _, player in pairs(players) do
local userId = player.UserId
local data = playerData[userId]
if data then
-- wrap in pcall to handle any errors
local success, result = pcall(function()
-- SetAsync yields so will stall shutdown
dataStore:SetAsync(userId, data)
end)
if not success then
warn(result)
end
end
end
print("completed saving player data")
end)
Roblox will not internally close a server until BindToClose has finished (or a 30 second timeout has been hit).
Without knowing how your logging system works this is difficult to advise you on.
It could be as simple as disabling your logging script or making it no longer detect logging once the :BindToClose function has been triggered as @vojta1587 and @FilteredDev have both said though
ipairs
is faster for arrays, and also used for arrays. pairs
is used mostly for dictionaries.
Since GetPlayers()
returns an array, use ipairs
over pairs
.