Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to get surrounding bricks of a certain basebrick?

connectedparts = script.Parent:GetConnectedParts(true)

for i = 1,5 do
	print (connectedparts[i].Position)
	if connectedparts[i].Position(connectedparts[i].Position.X < connectedparts[i].Position.X + 5,
		connectedparts[i].Position.Y < connectedparts[i].Position.Y + 5,
		connectedparts[i].Position.Z < connectedparts[i].Position.Z + 5) then
		
	
	connectedparts[i].Color = Color3.new(255,255,255)
	
	end
	
end

Hi there! im trying to figure out how i would get the surrounding bricks of a base brick and then paint those surround bricks a different color, in this example its white cause im not imaginative

1 answer

✓ Accepted answer

GetConnectedParts only gets the parts that are connected by any kind of joint. This will not find the surrounding baseparts. I recommend you use magnitude, by subtracting both position values and the magnitude property which will show the distance between the parts.

baseparts = workspace:GetChildren() --All the children in the workspace.

for i = 1,#baseparts do
if baseparts[i]:IsA("BasePart") and baseparts[i] ~= script.Parent then --The child is a part and isn't the script's parent.
    print (baseparts[i].Position)
	local distance = (script.Parent.Position-baseparts[i].Position).magnitude --Find the distance.
	if distance < 5 then --The part is less than 5 studs away.
    		baseparts[i].Color = Color3.new(255,255,255) --Paint the part white.
		end
    end
end

2 comments

0

Thanks!

0

I recommend using Region3.new(). Using a for loop like that is not efficient. Try to avoid loops as much as you want.

This discussion is preserved as it was. New answers, comments, and votes are closed.