Saving Player To Table

Can things like saving a player to a table and not clearing the table when they leave cause a data leak?

The table i’snt really expanding, though.

it’s like:

local Table = {Ply = Player}

And if that player leaves the game but you don’t clear the table, can it cause a leak?

Yes.

1 Like

You should bind Players.PlayerRemoving and clear the references to the player object when they the game. Most of the time, this involves deleting all references to the table containing the player key, not deleting the player key itself, because the table would also leak if you didn’t remove it.

5 Likes

Is it ok to clear the table?

Table = {}

or just make it nil?

Would that fix the leak?

Clearing the table or setting the player’s value to nil will clear up the memory.

You could be more advanced and use weak tables but it’s not for beginners.

2 Likes

Couldn’t we just make the value weak as so:

setmetatable(t, {__mode = "v"})
1 Like

Not without a script that constantly keeps a reference of every player.

Wait, in this case can this cause a data leak:

Part.Touched:connect(function()
local Table = {"a","b","c"}
end

everytime the part gets touched.

If so, how can I prevent it?

The table is cleared out of memory during garbage collection when the function finishes because it’s local.

1 Like

Whew.

Thanks.

1 Like

if you do

game.Debris:AddItem(Obj,10)

and by 10 seconds the Obj no longer exists bc it was deleted, can that cause a leak?

As long as you don’t have any other references to Obj, it’ll be garbage collected.