Garbage collection isn't working for me

  • I want to detect garbage collection in roblox.

  • When I use a weak table to reference a value, the values in it seemingly never get garbage collected.

  • I have searched through the DevForum. Most posts about garbage collection talked about using a weak table to detect garbage collection, but when I tested that, the values simply persisted in the table without getting garbage collected!

Script I made:

-- Make a weak table
local weakTable = {}
local metaTable = {}

metaTable.__mode = "kv"

setmetatable(weakTable, metaTable)

-- Add values
table.insert(weakTable, newproxy(true))

local testPart = Instance.new("Part")
table.insert(weakTable, testPart)

testPart:Destroy()
testPart = nil

-- Print the contents
script.Changed:Connect(function()
	print(weakTable)
end)

Script result:

Edit: I moved the script to another place, and the garbage collection suddenly started to work? I don’t know what is happening