I am looking to find a way to see how to detect neighboring parts of a part.
I am generating grass on top of some stone using a script.
This is the script:
local brickColors = {
BrickColor.new("Bright orange"),
BrickColor.new("Neon orange"),
BrickColor.new("CGA brown"),
BrickColor.new("Br. yellowish orange"),
BrickColor.new("Reddish brown"),
BrickColor.new("Cork")
}
for i, v in ipairs(workspace:GetDescendants()) do
if v.Name == "stone" then
local grass = v:Clone()
grass.Material = Enum.Material.Grass
grass.BrickColor = brickColors[math.random(#brickColors)]
grass.Size = Vector3.new(v.Size.X, 1, v.Size.Z)
grass.CFrame = v.CFrame * CFrame.new(0, (v.Size.Y / 2) + 0.5, 0)
grass.Parent = workspace
end
end
and this is how it looks:
The issue is that some parts neighboring parts are supposed to be the same color like these:
.
I do not want to detect if they have the same Y value because the map will extend farther and have varying elevations, some of which will have the same Y value as a part hundreds of studs away. I want to only detect neighboring ones.
I was thinking on doing a ray cast around all 4 sides of a part to see if there is a neighboring part with the same name and then change that parts color to the neighbor’s if it isn’t the same color (I have the parts named differently; stone & grass).
But the thing is i am thinking it will do a constant loop because let’s say two parts are checking each other and they’re constantly changing colors, they will keep changing colors.
That should work. Lets say the blue part is the one you are checking
The 4 black parts represent the raycasts (pointing down)
The green part was hit by a raycast, and the other three raycasts didn’t hit anything (because the part was farther away than the raycast distance)
You can check that the hit part is the same top height as the current part by using the Y size and Y position of the two parts
local currentPart -- part you are checking
local partColors = {} -- [Part] = Color3 - A table of part colors so you can get the color of the other part
-- Get a part's top height by doing this:
local function getTopHeight(part)
return part.Position.Y + part.Size.Y / 2
end
-- This part goes in your raycast code:
local color -- will be set to part color
local otherPartTopHeight = getTopHeight(otherPart) -- otherPart is the part that was hit by the raycast
if topHeight == otherPartTopHeight then -- parts are at the same height, use the same color
color = partColors[otherPart]
else -- parts are at different heights, use a random color instead
color = -- random color
end
partColors[currentPart] = color -- save color to table so other neighboring parts can get it
You’ll also need to use a whitelist in your RaycastParams that only includes the stone parts so it doesn’t accidentally hit anything else
That wouldn’t happen because you are only checking each part once. They wouldn’t be checking each other at the same time, they check one after the other (because it’s in a for loop).