Some parts not being effected by ray cast in loop?

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


The two white squares on the bottom left are what I am concerned about, sorry it goes by quick in the video.

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
1 Like
[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)
1 Like

This seems to work more consistently, all spots are effected, but for some reason only 3 colors are shown.


I don’t mean to completely rely on your code but I have no idea where the source of this is… :sweat:

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```

Can you show distance of all the parts which you printed and also what distance is orange color?

1 Like


orange is 100

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

error code, “Workspace.Script:41: attempt to compare nil <= number - Server - Script:41”

I am a little lost

v.Color = if distance >= data.Distance then data.Color else v.Color
1 Like

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.

1 Like

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.

1 Like

Thank you for your beautiful explanation and guidance

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.