I have a question

when you make datastores, would it be a good idea to use a system that stores every player’s userId in a table when the join the game, and then every five minutes or so, check through every player to see if their userid still exists, and if it doesn’t, then save the data and remove their userid from the table?

maybe something like this:

local Players = game:GetService("Players")
local dataTable = {}

Players.PlayerAdded:Connect(function(player)
table.insert (dataTable,1,player.UserId)
end)

while wait(300) do
local plrTable = Players:GetPlayers()
    for a = 1, #dataTable do
        for b = 1, #plrTable do
            if plrTable[b].UserId == dataTable[a] then
                  -- if the player is still in the game --
                 break -- move on to the next spot in the data table
            end
            if b = #plrTable -- if it looped through to the last spot of the data table
                if dataTable [a] ~= plrTable[#plrTable].UserId -- if it cannot find the last player's userId in the data table
                   -- save data here with that player's userid --
                table.remove( dataTable, a )
                end
            end
        end
    end
end)

could it work? Is it a good way to save the data? I want to know because I might use it.

You could use the Players.PlayerRemoving (official api page) event for this purpose (instead of periodically checking whether a player is leaving). It fires right before a players leaves the game. You can use it like this:

game.Players.PlayerRemoving:Connect(function(player)
   -- code to save leaving player data
end)

But you should also save player data (without removing its entry from the table) periodically when the player is still on the server, to minimize data loss.

1 Like

ok thank you i was just wondering.

1 Like