How should I return and change properties of parts around another part?

I am trying to have a part that, when in the vicinity of parts with a certain name, changes the partcolor of that part, and changes the properties of the children of that part. Currently I’m trying to use region3 based on the wiki page but I am not having any luck.

This is my code

local part = script.Parent
local min = part.Position - (0.5 * part.Size)
local max = part.Position + (0.5 * part.Size)
local region = Region3.new(min, max)
local parts = workspace:FindPartsInRegion3(region, part) --  ignore part


if parts.Name == "e81" then parts.BrickColor = BrickColor.new("Really black")
	
end

FindPartsInRegion3, as it says on its Developer Hub page, returns a list of BaseParts. Your code presumes it returns just one.

You can iterate over each part in this list and apply the brick color if the name of the part is equal to what you specify like so:

for _, part in pairs(parts) do
    if part.Name == "e81" then
        part.BrickColor = BrickColor.new("Really black")
    end
end

An alternative method you could use would be to compare the magnitude of the two positions between the parts.

2 Likes

you can use:

Child = game.Workspace:GetChildren()

for i, v in pairs(Child) do
    if v:IsA("Part") then
        if v.Name == "Thenameyouwant" then
            v.BrickColor = Instance.new("Colourthatyouwant")
        end
    end
end
1 Like

I think you should use if part:IsA("Part") then because what if you had a folder with the same name that would give an error

1 Like

It doesn’t seem to find the part even though it is definitely in it’s region3

Reply to FragmentFour.
May I suggest you put some prints in so you can see what is happening and where it stops.

1 Like

Building on @VegetationBush’s solution, you can loop through each descendant Workspace (higher Instances = slower):

local part = script.Parent

for _, Descendant in ipairs(workspace:GetDescendants()) do
	if Descendant.Name == "e81" and Descendant:IsA("BasePart") then
		if (Descendant.Position - part.Position).magnitude <= 5 then --put your range
			Descendant.BrickColor = BrickColor.new("Really black")
		end
	end
end