How would you get nearby parts without lagging the server?

  1. What do you want to achieve?
    I want to get all nearby parts to another part in a quick manner. This is done to delete parts that are too close to another player’s parts.

  2. What is the issue?
    There’s a noticeable delay when placing parts on my build tool when there are a large amount of parts present within the server.

  3. What solutions have you tried so far?
    I’ve been going through every part in the game that shares a parent with other player’s parts, and checking the magnitude of each one of those parts. If the part is too close, it gets deleted.

	for i,v in pairs(PlayerParts.Parent:GetDescendants()) do --If part is placed 10 studs near a part that doesn't want other parts, it gets deleted
		if v:IsA("BasePart") then
			if not v:IsDescendantOf(workspace["PlayerParts"]:FindFirstChild(Player.Name..'s Blocks')) then -- make sure player doesn't own part
				if v:FindFirstChild("deleteOtherParts") then -- a check to see if that part has deleting enabled
					if (part.Position - v.Position).Magnitude < 10 then -- is the part within 10 studs?
						part:Destroy()	--Destroy the part so it does not get created
					end
				end
			end
		end
	end

This creates a noticeable replication delay which can hinder a player’s experience.
What would be a quicker way to find nearby parts?

You can use Region3.

local Part = -- Link to the part whose surroundings you want to check.

local BotttomLeft = Part.Position - Vector3.new(10, 10, 10)
local TopRight = Part.Position + Vector3.new(10, 10, 10)
local Region = Region3.new(BottomLeft, TopRight)
-- This region3 is 10 studs bigger than Part on all sides.

Region:ExpandToGrid(4)

for i, v in pairs(game.Workspace:FindPartsInRegion3(Region)) do
    print(v.Name .. " is within ten studs of " .. Part.Name)
end

WorldRoot:FindPartsInRegion3

1 Like

Seems to work flawlessly, thanks!