Models in a bounding box

Hi! So to keep things short, how would I go about finding models (only in a certain folder) in a bounding box, (like a region3)

  1. What is the issue? I have been trying to find an efficient method to do so but i am simply unable to find something that doesn’t use a lot of resources, by looping through tens of thousands of parents of the parts.

  2. What solutions have you tried so far? I tried using parts in region3 (depricated) and Worldroot:Partsinbounding box, none of these do what I am looking for, as they spit out parts, and I want to delete whole models inside of the bounding boxes

It would be wonderful to have the script optimized as it is supossed to be main part of the game, and involves deleting a LOT of models in shorst amount of time, so llooping through parents is extremely non-ideal. Ty! :smiley:

I would use :GetPartsInPart, and :FindFirstAncestorWhichIsA(“Model”).

Code:

--//Variables
local BoundingBox = workspace.BoundingBox

--//Functions
for i, part in ipairs(workspace:GetPartsInPart(BoundingBox)) do
	if not part:IsDescendantOf(workspace) then
		continue
	end
	
	local model = part:FindFirstAncestorWhichIsA("Model")
	
	if model then
		model:Destroy()
	end
	
	--//Add task.wait() if script freezes game (should only happen if there are ALOT of parts)
end

You can make it a lot more optimized by utilizing OverlapParams, but you didn’t include any details about the models you wanted to remove, so I couldn’t add it in.

1 Like

Also, I’m not entirely sure if that part:IsDescendantOf(workspace) is necessary, I only added it just incase it looped over parts that were already destroyed.

Yo actually i didn;t think of that tysm

1 Like

No problem, if you need it to be optimized more, I’m free to help.

1 Like

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