How To Stop Vanishing Of Data In A Module Script [Help Needed!]

Hey There, I Was Making A Regestring System For A Specific Purpose,
I Used A ModuleScript And Here’s The Code In It:

local module = {}

local Parent = script.Parent
local registeredParts = {}

local Rand = Random.new()

local function getNewRandResult()
	while true do
		local RR = Rand:NextNumber(0, 100000000)
		if registeredParts[tostring(RR)] == nil then return RR end
	end
end

module.Register = function(basePart: BasePart): RegisterCode
	if basePart == nil then return end
	
	local RandRs = getNewRandResult()
	registeredParts[tostring(RandRs)] = basePart
	
	print(registeredParts)
	
	return RandRs
end

module.getRegisteredParts = function()
	print(registeredParts)
	return registeredParts
end

module.Unregister = function(key: number)
	if typeof(key) ~= "number" then return end
	
	registeredParts[tostring(key)] = nil
end

return module

When I Call The Register Method From Outside, It Outputs:
{ [<a random number>] = TestPart }
Since The Part’s Name Was TestPart
After That, I Called The getRegisteredParts Method, Is Outputs:
{}
Yes, An Empty Table, It Means That The Table Has Been Changed
When I Replaced print(registeredParts) on both the method to print(tostring(registeredParts))
So It Will Print The Memory Code For This table, And Intrestingly,
In The Register Method, It Printed Something Else Than In The getRegisteredParts Method.
I am not sure why and whose ghost is changing the table.
If Anyone Knows, Please Let Me Know, Thanks!

Is RegisterCode your own custom type?? I know by the looks of it, but what’s the type code?

Have you tried to instantiate a new key pair with the directory method?

local RandRs = tostring(getNewRandResult())
registeredParts.RandRs = basePart

I’m just throwing out this idea, it may or may not be the solution.
You could also try:

local RandRs = tostring(getNewRandResult())
registeredParts[RandRs] = basePart

It Is Not A Custom Type, And I Am Not Using any OOPs

RandRs Is Not A Property Or Element Of registeredParts Table As You Did registeredParts.RandRs, RandRs Variable Is Only Used So Even By Chance The Random Number Doesnt Repeat Cuz That Random Number To String Is The Key To Store Data In The registeredPart Table, The getNewRandResult Method Doenst Matters At All!

Alright so I just tested your module in an empty baseplate, and it seems like you have Unregister() method being performed. Check to make sure it’s not being called.
image

Here’s the code I used to run your module:

local registrar = require(game.ServerScriptService.ModuleScript)

registrar.Register(workspace.Part)
local parts = registrar.getRegisteredParts()

No, I’ve Checked 2 Times And The Unregister method is not being called.

Ok, So, I Figured Out Something, I Was Testing, Like Registring A Part On Script1, Ok Done, And In Script2 I Was Calling wait(2) for waiting then printing the result of getRegisteredParts Method So It Will Register First On Script1 Then It Will Print After 2 Seconds on Script2, And Bingo! It Worked, I Was Looking At My Script Carefully And Noticed That The Script That Was Registering That Part Has No Actor, But The Script Calling getRegisteredParts Method Was On An Different Actor. In My Testing I Told About Above, I Put The Script2 In An Actor, And Gotcha! It Wasn’t Working Anymore. I Figured Out That These Script Are Running On Different Threads Due To Script2 Being On An Actor, I Needed That Actor To Run Task On A Different Thread. So Destroying The Actor Isnt A Solution To me. Do Any Of You Have Any Idea How And Why If Scripts Are On Different Thread, They Are Not Sharing The Same Data And How I Fix This? Please Give Your Suggestions.