Clearing non-static function variables

local function OnPlayerAdded(Player)
    local Character = Player.CharacterAppearanceLoaded:Wait()

    Character = nil
    Player = nil
end

Players.PlayerAdded:Connect(OnPlayerAdded)

Is it necessary to make Player and Character nil after the code using those variables is done?

1 Like

Nope! Lua will clear them up automatically when the scope is exited.

3 Likes

collectgarbage() is ran when there are unnecessary memory, therefore prevents memory to increase indefinitely. Woops, someone clarified to me something about it.

Unfortunately, we cannot control it. We can only read from it.

1 Like

I’m pretty sure collectgarbage is just a function that developers can run to interface with the garbage collector and control or read from it, not something that’s actually ran (the garbage collector being an independent process).

Correct me if I’m wrong.

1 Like