This part randomly teleports around this baseplate. It is supposed to effect every part in the folder differently depending on how close it is. But for some reason some of the parts aren’t effected, as you can see in the video, some of the parts that should be pink are white.
local glitch = workspace:FindFirstChild("Glitch")
local folder = workspace.glitchParts
while wait(1) do
glitch.Position = Vector3.new(math.random(-250,250),6,math.random(-250,250))
for _, v in ipairs(folder:GetChildren()) do
local rayOrigin = (v.Position)
local raydirection = (glitch.Position - v.Position)
local raycastResult = workspace:Raycast(rayOrigin, raydirection)
local distance = raycastResult.Distance
if distance >= 0 then
v.Color = Color3.new(1, 1, 1)
if distance >= 50 then
v.Color = Color3.new(1, 1, 0.592157)
if distance >= 100 then
v.Color = Color3.new(1, 0.701961, 0.341176)
if distance >= 200 then
v.Color = Color3.new(1, 0.329412, 0.329412)
if distance >= 300 then
v.Color = Color3.new(1, 0.184314, 0.713725)
end
end
end
end
end
end
end
First of all, I’m assuming you want the parts to be colored differently based on how close they are to the glitchPart. So with that in, you really don’t need to raycast to find the distance between both of the parts. Something as simple as this should work.
local distance = (glitchPart.Position - v.Position).Magnitude
Second of all use tables,
local distanceMap = {
0 = Color3.new(0, 0, 0),
50 = Color3.new(1, 1, 0.592157),
100 = Color3.new(1, 0.701961, 0.341176),
200 = Color3.new(1, 0.329412, 0.329412),
300 = Color3.new(1, 0.184314, 0.713725)
}
while true do
glitch.Position = Vector3.new(math.random(-250,250),6,math.random(-250,250))
for _, v in ipairs(folder:GetChildren()) do
local distance = (glitchPart.Position - v.Position).Magnitude
for dist, color in distanceMap then
v.Color = if distance >= dist then color else v.Color
end
end
end
I don’t mean to completely rely on your code but I have no idea where the source of this is…
local glitch = workspace:FindFirstChild("Glitch")
local folder = workspace.glitchParts
local distanceMap = {
[0] = Color3.new(1, 1, 1),
[50] = Color3.new(1, 0.917647, 0),
[100] = Color3.new(1, 0.45098, 0),
[200] = Color3.new(1, 0, 0),
[300] = Color3.new(1, 0.184314, 0.713725)
}
while wait(1) do
glitch.Position = Vector3.new(math.random(-250,250),6,math.random(-250,250))
for _, v in ipairs(folder:GetChildren()) do
local distance = (glitch.Position - v.Position).Magnitude
print(distance)
for dist, color in distanceMap do
v.Color = if distance >= dist then color else v.Color
end
end
end```
local glitch = workspace:FindFirstChild("Glitch")
local folder = workspace.glitchParts
local distanceMap = {
{Distance = 0, Color = Color3.new(1, 1, 1)},
{Distance = 50, Color3.new(1, 0.917647, 0)},
{Distance = 100, Color3.new(1, 0.45098, 0)},
{Distance = 200, Color3.new(1, 0, 0)},
{Distance = 300, Color3.new(1, 0.184314, 0.713725)}
}
while wait(1) do
glitch.Position = Vector3.new(math.random(-250,250),6,math.random(-250,250))
for _, v in ipairs(folder:GetChildren()) do
local distance = (glitch.Position - v.Position).Magnitude
print(distance)
for _, data in distanceMap do
v.Color = if data.Distance >= dist then data.Color else v.Color
end
end
end
I also added Color = to each line of the table, and now it works PERFECTLY!!! Thank you so much for your time and patience with me. Have a beautiful day.
What happened previously was that dictionaries aren’t traversed sequentially i.e it won’t check with first 0 then 50, then 100 and so on. The order was unpredictable, so that’s why I switched over to arrays.