Will this cause a memory leak?

I have made a simple cooldown system:

if (playerCoolDown[plr]) then return end
playerCoolDown[plr] = true

But will this code cause a memory leak if the player leaves? Do I have to manually set

playerCoolDown[plr] = nil

when the player leaves or will it get garbage collected automatically?

1 Like

Yes, you have to. Alternatively you can use nil instead of false by default since both evaluate to false in a comparison. Another alternative is setting __mode of your table to “k”.

How does that work? I don’t know much about metatable functions.

Making a table weak means that references inside the table won’t hinder GC like they usually do. In this case “k” means weak keys (your plr variable).

1 Like

When the player leaves, it is always best practice to set any references of them to nil. So, yes, you should do playerCoolDown[plr] = nil when they leave!

On its own, that won’t cause a memory leak and such a tiny amount of data (a boolean) being stored in a table won’t be a big deal if you forget to clean it up. I think we need more context to say whether or not your script will memory leak (although, from just that, it will not).

1 Like

I looked in the __mode documentation and found this:

Used in weak tables, declaring whether the keys and/or values of a table are weak. NOTE: References to Roblox instances are never weak. Tables that hold such references will never be garbage collected.

So the __mode method will not work here right? So I guess I will have to go with manually setting them to nil.

Refer to this other post of mine:

I tried your script:

local weakTable = setmetatable({},{__mode = "v"})
table.insert(weakTable, workspace.Part)
workspace.Part:Destroy()
wait(1)
print(next(weakTable)) --prints nil; if __mode wasnt set, it would have printed the instance

But it printed the part’s name, (I ran it in the command bar by the way while the game was running.).


:man_shrugging:
Regardless I still recommend just setting it to nil by default instead of false. The performance cost of allocating memory for the boolean for each cooldown activation should be insignificant.

My article covers weak tables as well if you’re interested

Screenshot_1

For me it did this for some reason.

You used __mode = “k” instead of “v”. workspace.Part is not a key here, but a value since I used table.insert.