I have a table and multiple variables that are used for connections, when I’m no longer using them, should I clear the table, and set both the table and connection variables to nil?
Example:
local glassCrackDecals = {
bombClone["Crack1"];
bombClone["Crack2"];
bombClone["Crack3"]
}
for i = 1, 3 do task.wait(bombData.tickTime)
tickSound:Play()
bombClone["Crack"..i].Transparency = 0
pitchCorrection.Octave += .5
if i == 3 then task.wait(1) end
end
pitchCorrection:Destroy()
tickSound:Destroy()
if plant == false then
bombClone.Anchored = true
bombClone.Transparency = 1
for i, v in glassCrackDecals do
v:Destroy()
end -- Table is now empty, so should I just set its variable to nil?
end
If you plan to use it again as a table, do not set it to nil. Instead, just keep it as an empty table. If there is no more purpose for it, ig you can set it to nil
I heard about garbadge collection and I’m not sure how it works so I’m thinking I don’t have to, but I’m not sure if after like some time of inactivity it will delete it. I don’t want there to just be data lying around that I’ve already finished using. Note that this is a table from a module that’s going to be used repetitively.
If you’re gonna use it a lot then keep it around. Protect it from garbage collection by adding some random value to it (idk if leaving it as an empty table is enough)
it works this way if there is any refference to object in table, it cannot be collected, soo remember to remove metatables, instances and propertiess of this table before removing it
The only scenario I can think of is if you don’t plan to reuse the variable but the scripting environment thinks you will, so it doesn’t garbage collect it. I assume something like that can happen if the variable is in an incorrect place(for example a global variable that’s only used by a specific function).
In general if you write clean code you should not worry about directly setting things to nil in order to remove them from memory, the garbage collector should do it automatically.
bomb.Default = function(user, bombData, bombClone, plant, debugMode)
local tickSound = Instance.new("Sound")
tickSound.SoundId = bombData.tickSound
tickSound.Parent = bombClone
for i = 1, 3 do task.wait(bombData.tickTime) -- The timer on the bomb for explosion is ticking
tickSound:Play()
if i == 3 then task.wait(1) end -- prevents the bomb from exploding instantly on the third i = (tickTime * 3) + 1
end
if plant == false then
bombClone.Anchored = true
bombClone.Transparency = 1
end -- We anchor the bomb so that it doesn't roll awaybomb.Anchored = true -- We anchor the bomb so that it doesn't roll awaw
bombClone:SetAttribute("Exploded", true)
local explosionSoundClone = Instance.new("Sound")
explosionSoundClone.SoundId = bombData.explosionSoundId
explosionSoundClone.Name = "explosionSound"
explosionSoundClone.Parent = bombClone
explosionSoundClone:Play()
local explosionClone = explosionParticles(bombClone, bombData)
explosionClone:Emit(100 + (bombData.blastPower / 2))
local region3 = Region3.new( -- Creates a region3 that destroys any parts in the explosions radius.
bombClone.Position - Vector3.new(bombData.range.X / 2, bombData.range.Y / 2, bombData.range.Z / 2),
bombClone.Position - Vector3.new(bombData.range.X / 2, bombData.range.Y / 2, bombData.range.Z / 2)
) -- Divide the range values by two to compensate for the size created by the region3 which is multiplied by two (e.x 16st = 32st)
local explosionVisualizer
if debugMode then explosionVisualizer = visualizerCalculator(region3) end
local overlapParams = OverlapParams.new()
if debugMode then overlapParams.FilterDescendantsInstances = {explosionVisualizer} end
local objectsInBlastRadius = workspace:GetPartBoundsInBox(region3.CFrame, region3.Size, overlapParams)
applyForceOnObjects(user, objectsInBlastRadius, bombClone, bombData)
debris:AddItem(bombClone, 5)
debris:AddItem(explosionVisualizer, 5)
end