How do I .magnitude hundreds of parts at a time without causing too much lag?

I’m trying to find a better way to script this. I’m sure I could figure out a better way through trying different things, but I figured someone would know and save me some time. Thanks.

while true do
	wait(1)
	for i,v in pairs(game.Workspace.Items:GetChildren()) do
		local magnitude = (v.Position - script.Parent.Handle.Position).magnitude
		if magnitude <= 8 then
			v:Destroy()
		end
	end
end

You could instead try making a Region3 around your handle and see what parts are in the Region3 and add in a filter to ignore parts you don’t want destroyed.

1 Like

You could try using FindPartsInRegion3WithWhiteList

If that doesn’t work you could create your own grid system for storing item locations and only check nearby locations in the grid.

6 Likes

Genius, thanks so much guys!

Just note that Region3 does not include rotation.

I think there’s also a limit to how many parts you can find at a time with Region3’s. Let us know if this is a problem for you, there might be ways of working around it.

It would be easier to help you find a good solution to your problem if you tell us what end goal you’re trying to achieve here - there may be a better way altogether.

3 Likes