Help me with my code for module

The issue is after unloading the object the object unloads and never loads back.
I tried replacing registeredcframes table with attributes, tried lower unload cframe, debugged everything, overall did everything to resolve this issue but couldnt resolve it please help.
Here’s the code of my module(it’s gonna be opensource):

-- Created by 1Xayd1
local RenderCache = {}
RenderCache.__index = RenderCache

export type RenderCacheType = {
	MovingParts: {BasePart},
	MovingCFrames: {CFrame},
	RegisteredCFrames: {[BasePart]: CFrame},
	unloadCFrame: CFrame,
	Updating: boolean,
	new: () -> RenderCacheType,
	Register: (self: RenderCacheType, array: {BasePart}) -> (),
	Update: (self: RenderCacheType) -> (),
	Load: (self: RenderCacheType, Part: BasePart) -> (),
	Unload: (self: RenderCacheType, Part: BasePart) -> ()
}

function RenderCache.new(): RenderCacheType
	local self = setmetatable({}, RenderCache)
	self.MovingParts = {}
	self.MovingCFrames = {}
	self.RegisteredCFrames = {}
	self.unloadCFrame = CFrame.new(2^24, 2^24, 2^24)
	self.Updating = false
	return self
end

function RenderCache:Register(array: {BasePart})
	for _, part in ipairs(array) do
		self.RegisteredCFrames[part] = part.CFrame
	end
end

function RenderCache:Update()
	if self.Updating then warn("Tried to update while updating, skipping.") return end
	self.Updating = true
	workspace:BulkMoveTo(self.MovingParts, self.MovingCFrames, Enum.BulkMoveMode.FireCFrameChanged)
	table.clear(self.MovingParts)
	table.clear(self.MovingCFrames)
	self.Updating = false
end

function RenderCache:Load(Part: BasePart)
	if not self.RegisteredCFrames[Part] then
		warn("Part not registered in cache!")
		return
	elseif self.Updating then
		warn("Cannot load | unload objects while updating.")
		return
	end

	table.insert(self.MovingParts, Part)
	table.insert(self.MovingCFrames, self.RegisteredCFrames[Part])
end


function RenderCache:Unload(Part: BasePart)
	if not self.RegisteredCFrames[Part] then
		warn("Part not registered in cache!")
		return
	elseif self.Updating then
		warn("Cannot load | unload objects while updating.")
		return
	end
	
	table.insert(self.MovingParts, Part)
	table.insert(self.MovingCFrames, self.unloadCFrame)
end

return RenderCache

I have no idea what is causing issue whatsoever :face_with_diagonal_mouth:

1 Like
function RenderCache:Register(array: {BasePart})
	for _, part in ipairs(array) do
		self.RegisteredCFrames[part] = part.CFrame
	end
        print(#self.RegisteredCFrames) -- will print 0 
end
1 Like

Can you clarify what you mean by this? Are you referring to the Unload and Load functions and if so, what is the expected behavior when you load an object back?

 print(#self.RegisteredCFrames) -- will print 0 

As for this, you’re using RegisteredCFrames as a dictionary and not an array. Since there are no indexed elements, #self.RegisteredCFrames will always be 0 even if there are elements in the table.

1 Like

Yes i am reffering to load and unload functions. i said: when module unloads an object and tries to load it back its never gonna do so.

1 Like

Do you run this code on the server or client? If it’s on the client, my only guess is that when you unload an object, you move it so far away that it gets streamed out and when you try and move it back, it no longer exists.

Other than that, I don’t really know why it wouldn’t be working. Are you seeing errors or logs or anything? My only other suggestion would be to print the various tables when you call Load, Unload, or Update and confirm the data is how you expect it to be.

Client ofcourse i have a module for culling, i wrote this one as a library to render and unrender thing super fast.

Yeah i tried everything but still cant see why its not working too

You’ve tried disabling streaming too?

Oh wait i didnt that might be it though! I hqte streaming so much btw its breaking things and just looks ugly thats why i am developing blazing fast cull module for this.

Its working on tags and you can choose which objects it culls

I dont think theres another explanation why its happening, anyways i am gonna go sleep

Yea I can’t think of any other reason why this would be happening either

1 Like

nope its not streaming either
filler text: tex text

Well i guess RenderCache not happening…

?
filler text: 123455556789012345678

When you’re unloading you’re not removing the old position CFrame, you have both the void and ‘default’ in the same table.

Update the old CFrame when unloading (currently you’re pushing a new one)

1 Like

As i said i tried working with attributes, attribute stored original cframe and it was static i checked, but again, it unloaded the part and never loaded it back. But I will try

What happens is that when load and unload the same object before a update the table is not cleared, this makes so there’s 2 registers of the same part before the update.

Load → Adds the position and part to the table
Unload → Also adds the unload position and part to the table

edit: you should check in the unload function, to see if the part was added already to the table and if so edit the cframe to the void one

1 Like

like so?

for i, movingPart in ipairs(self.MovingParts) do
		if movingPart == Part then
			self.MovingCFrames[i] = self.unloadCFrame
			return
		end
	end