How can I delete objects not inside a table?

I already have a table of Minecraft-like chunks in a table that should be loaded, because they are within a certain radius of the player.

what I need to do is delete chunks that are not in this table, all the chunks are in a folder.


Either move the parts in a table to another folder temporarly and delete the children left or just run thro all the contents and check if its in the table or not and delete those that arent.


yeah how can I check if it’s not in the table? that’s all I really need but not sure. thanks

if table.find(table,Object) then
 --- Its in the table

Table.find returns the index value of whatever your looking for, so wont return the actual object, so you still need to use that to get the object from the table to delete it to prevent mishaps


Maybe you could try this:

local LoadedChunks = {YourTable}
local ChunksFolder = --Folder goes here ig
local NotLoadedChunks = {}
local function GetNotLoadedChunks()
for _, NotLoadedChunk in ChunksFolder:GetDescendants() do --Gets all the chunks
for _, LoadedChunk in LoadedChunks do --Gets all the loaded chunks
if NotLoadedChunk ~= LoadedChunk then --If the chunk is not in the loaded chunk table then it inserts it into the not loaded chunk table

And then with the NotLoadedChunks table you could do whatever with them let me know if this helps you!

I believe you don’t need to redefine the object, as you have the object value as the needle argument for table.find. The variable is already there and doesn’t need an extra indexation.

lol, if i give you a list full or randomly created parts you cannot find it without using table.find, and as always you cannot access the tables index without that. As for calling table.find twice that is actually completely the opposite of what you said, its to avoid defining a new object/variable to save on memory.

Anyways @shadowbendy123 has a more relevant post to rendering than the simplfied way i did, so id reccomend using that as a more acurate referance for your script.

I believe you misunderstood my original point, which is understandable as I didn’t word it very well. In your original post, you’re fetching the instance twice. You already have a defined Object variable which implies it is defined in a loop. It is unnecessary to do this:

You can just call Object:Destroy(). You’re not saving on any memory here at all, and instead are calling an extra function unnecessarily.

Object:Destroy() works if you have an instance to perform the function so you arent wrong on that one and yes in this case memory optimising isnt needed yet and it can even be further reduced while inside the loop.

As for the whole memory thing, this is some stuff you wouldnt normally worry about too much unless defining lots of variables or holding lots of info on them, In well made scripts the memory gives out before the script usage especially in cases where you are passing part descriptions or storing masses of parts from maps and chunks. Thats a different can of worms and is getting more off topic to sorting an array so ill leave it there.

1 Like

The code that keeps track of what chunks should be shown should store the “currently shown” chunks in a table. Every time that changes, you only have to check chunks in that table to see if they should no longer be shown.

1 Like

ended up scrapping the table and solving with loops. here’s what I thought of that works. thanks everyone.


This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.