Would this get garbage collected

local TweenData = TweenInfo.new(0.25,Enum.EasingStyle.Linear,Enum.EasingDirection.In, 0, false, 0)
local Tween = TweenService:Create(Effect,TweenData,{Size = Vector3.new(25,0.2,25)})
Tween:Play()

Then the script would end there, does tween and tweendata need to be set to nil or is it automatically garbage collected?

1 Like

local variables get garbage collected once it reaches the end of its code block with end

Unless there’s an event listener in the same code block sharing the same env

3 Likes

So say I have a module script that looks for sounds would I need to move it inside so it gets garbage collected?

local Sounds = Storage.Sounds
local EffectsModule = {}

EffectsModule.Fire = function(P)
local Sound = Sounds.Grab:clone()
Sound.Volume = Sounds.Volume
Sound.Parent = P.HumanoidRootPart
Sound:Destroy()
end

return EffectsModule

I mean there’s no reason to garbage collect it. The variable needs to be used multiple times but doesn’t need to be changed

I think you’re over looking garbage collecting, variables don’t take up that much memory and the main reason for memory leaks are too many event listeners.

when a thread gets created, by using :Connect() or spawn its env (environment) which includes all global variables and local variables (stored above) stored on the script and then gets garbage deleted once it gets to end

Primitive types don’t get garbage collected except for reference types assuming they have 0 references when the garbage collect runs in the next cycle.

local a = 1
-- scope ends, a wont be garbage collected because it doesnt have to
3 Likes

How about tables will I need to always set them to nil afterwards?