I want a function that checks if there are 2 chunks overlapping each other and destroy one of them so it doesnt cause lag. Right now I have a random terrain generation thing and I made it able to load, and destroy chunks based off of a render distance. But whenever new chunks load, it overlaps the chunks that are alraedy loaded it which causes lots of lags and can slow down the loading process.
I have tried lots of different functions but none would work. The function I currently use is a function that is run in a while loop which also calls the functions that create and destroy the chunks, it basically just checks if there are chunks. It sort of works, but only on 1 chunk. When I test it myself, only 1 chunk is not being overlapped but I want it to do that with all of the chunks that are overlapped, not just one.
function existCheck()
for _, v in pairs(map:GetDescendants()) do
for _, code in pairs(map:GetDescendants()) do
if v:FindFirstChild("Code").Value == code:FindFirstChild("Code").Value then
v:Destroy()
return true
end
end
end
return false
end
Each chunk has it’s own code. The code is the X position of it’s main part, multiplied but its Z position. And in that code, it is supposed to check if there are 2 chunk codes that are the same. I did some checking and I saw that each chunk has their own code and the chunks that overlap have duplicate codes which this function is supposed to identify then destroy.