Hey so i have a game with 10 rounds, and at the end of the 10 i want to completely destroy the table
If the table only exists in memory the simplest thing to do would be to set the variable pointing to the table to nil then let the garbage collector clean it up.
If you have persisted the table somewhere such as a data store then setting the key to nil will remove the entry from the store.
You might want to do both.
Here is my mainframe script. As you can see the table is PlayersInServer. I want to clear the table or else the game takes the archived table for the next set of rounds
local Players = game:GetService("Players")
repeat
task.wait(.2)
until (#Players:GetPlayers() > 0)
print("player assets loaded")
--code here runs after above condition is met, you can start the timer
-- GAME 1 --
while true do
local RS = game:GetService("ReplicatedStorage")
local Colour = RS:WaitForChild("Colour")
local PlayersService = game:GetService("Players")
local PlayersInServer = PlayersService:GetPlayers()
local StarterBlock = workspace:WaitForChild("StarterBlock")
local SquareFolder = workspace:WaitForChild("Squares")
local PlrAddConn, PlrRemoveConn, CharAddConn, CharRemoveConn, DiedConn = nil, nil, nil, nil, nil
PlrConn = PlayersService.PlayerAdded:Connect(function(Player)
table.insert(PlayersInServer, Player)
if PlrRemoveConn then
PlrRemoveConn:Disconnect()
end
CharAddConn = Player.CharacterAdded:Connect(function(Character)
if DiedConn then
DiedConn:Disconnect()
end
if CharRemoveConn then
CharRemoveConn:Disconnect()
end
local Humanoid = Character:WaitForChild("Humanoid")
DiedConn = Humanoid.Died:Connect(function()
if table.find(PlayersInServer, Player) then
table.remove(PlayersInServer, table.find(PlayersInServer, Player))
end
end)
end)
local CharRemoveConn = Player.CharacterRemoving:Connect(function(Character)
if CharAddConn then
CharAddConn:Disconnect()
end
end)
end)
local Timer = 51
for i = Timer, 0, -1 do
task.wait(1)
print(i)
end
StarterBlock.Transparency = 0
StarterBlock.CanCollide = true
task.wait(2)
for i = 1, 3 do
local Circles = SquareFolder:GetChildren()
Chosen_Colour = Circles[math.random(1, #Circles)]
Colour.Value = Chosen_Colour.Name
task.wait(6)
StarterBlock.Transparency = 1
StarterBlock.CanCollide = false
task.wait(2)
for number,Circle_Colours in pairs(Circles) do
for number,Circle_Part in pairs(Circle_Colours:GetChildren()) do
Circle_Part.Transparency = 1
Circle_Part.CanCollide = false
end
end
for number, Circle_Part in pairs(Chosen_Colour:GetChildren()) do
Circle_Part.Transparency = 0
Circle_Part.CanCollide = true
end
task.wait(3)
for number,Circle_Colours in pairs(Circles) do
for number,Circle_Part in pairs(Circle_Colours:GetChildren()) do
Circle_Part.Transparency = 0
Circle_Part.CanCollide = true
end
end
end
for i=1,2 do
Circles = SquareFolder:GetChildren()
Chosen_Colour = Circles[math.random(1,#Circles)]
Colour.Value = Chosen_Colour.Name
task.wait(5)
for number,Circle_Colours in pairs(Circles) do
for number,Circle_Part in pairs(Circle_Colours:GetChildren()) do
Circle_Part.Transparency = 1
Circle_Part.CanCollide = false
end
end
for number, Circle_Part in pairs(Chosen_Colour:GetChildren()) do
Circle_Part.Transparency = 0
Circle_Part.CanCollide = true
end
task.wait(3)
for number,Circle_Colours in pairs(Circles) do
for number,Circle_Part in pairs(Circle_Colours:GetChildren()) do
print("h")
Circle_Part.Transparency = 0
Circle_Part.CanCollide = true
end
end
end
for i=1,3 do
Circles = SquareFolder:GetChildren()
Chosen_Colour = Circles[math.random(1,#Circles)]
Colour.Value = Chosen_Colour.Name
task.wait(3)
-- Round 6,7,8
for number,Circle_Colours in pairs(Circles) do
for number,Circle_Part in pairs(Circle_Colours:GetChildren()) do
Circle_Part.Transparency = 1
Circle_Part.CanCollide = false
end
end
for number, Circle_Part in pairs(Chosen_Colour:GetChildren()) do
Circle_Part.Transparency = 0
Circle_Part.CanCollide = true
end
task.wait(3)
for number,Circle_Colours in pairs(Circles) do
for number,Circle_Part in pairs(Circle_Colours:GetChildren()) do
print("h")
Circle_Part.Transparency = 0
Circle_Part.CanCollide = true
end
end
end
Circles = SquareFolder:GetChildren()
Chosen_Colour = Circles[math.random(1,#Circles)]
Colour.Value = Chosen_Colour.Name
task.wait(2)
for number,Circle_Colours in pairs(Circles) do
for number,Circle_Part in pairs(Circle_Colours:GetChildren()) do
Circle_Part.Transparency = 1
Circle_Part.CanCollide = false
end
end
for number, Circle_Part in pairs(Chosen_Colour:GetChildren()) do
Circle_Part.Transparency = 0
Circle_Part.CanCollide = true
end
task.wait(3)
for number,Circle_Colours in pairs(Circles) do
for number,Circle_Part in pairs(Circle_Colours:GetChildren()) do
print("h")
Circle_Part.Transparency = 0
Circle_Part.CanCollide = true
end
end
task.wait(2)
StarterBlock.Transparency = 0
StarterBlock.CanCollide = true
Colour.Value = "Game ended."
for _, player in ipairs(PlayersInServer) do --change "PlayerList" to name of table alive players are stored inside
local leaderstats = player:WaitForChild("leaderstats")
if leaderstats then
print("win was given")
local Wins = leaderstats:WaitForChild("Wins")
if Wins then
Wins.Value += 1
end
end
end
------ WHERE THE DESTROY TABLE SCRIPT SHOULD GO
task.wait(2)
Colour.Value = " "
end
As far as I can see you shouldn’t need to do anything, in each iteration of your while loop you are resetting PlayersInServer with a call to PlayersService:GetPlayers(), which should be giving you a reference to a new table containing the current list of players in the server. If you really want to be explicit you could set PlayersInServer to nil at the point where your comment indicates you want to destroy the table. This will clear the reference and allow the memory to be released by the garbage collector.
On a different subject you should review your code where you connect to player events such as added. This would be better outside the loop then you wouldn’t have to worry about deleting connections. As it is you are creating a new connection to player added on every iteration but not disconnecting it.
Just to make it super clear why this is bad: that leaks memory because each “copy” of the listener functions (or their closures, really) have to stay around in memory, and even worse it probably results in bugs that can be pretty hard to find.
to destroy a table, simply set the table variable to {}
or nothing
like this,
local tableVariable = {}
← this is if you want to the type of variable to remain a table but become an empty table
local tableVariable = nil
← this sets the variable to nil
(basically nothing)