Help with for loop

Hey. So basically I have an EMF Meter system, with magnitude and stuff. So I have multiple parts in a folder in workspace. Im trying to get it so it does the magnitude thing to every part instead of just one. Right now with the code below, it only chooses one even though I do a for loop for it.

while true do

	local char = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()
	
	for i,v in pairs(workspace.GhostMagnitudeThingies:GetChildren()) do
			
			distance = (char:WaitForChild("HumanoidRootPart").Position - v.Position).magnitude

			value = math.floor(math.clamp(50 - distance, 0, 50))

	end
	
	if value > 0 and value < 10 then

		script.Parent.L1.Material = Enum.Material.Neon
		script.Parent.L1.Transparency = 0

	else

		script.Parent.L1.Material = Enum.Material.Glass
		script.Parent.L1.Transparency = 0.5

	end

	if value > 10 and value < 20 then

		script.Parent.L1.Material = Enum.Material.Neon
		script.Parent.L1.Transparency = 0

	else

		script.Parent.L2.Material = Enum.Material.Glass
		script.Parent.L2.Transparency = 0.5

	end

	if value > 20 and value < 30 then

		script.Parent.L3.Material = Enum.Material.Neon
		script.Parent.L3.Transparency = 0

	else

		script.Parent.L3.Material = Enum.Material.Glass
		script.Parent.L3.Transparency = 0.5

	end

	if value > 30 and value < 40 then

		script.Parent.L4.Material = Enum.Material.Neon
		script.Parent.L4.Transparency = 0

	else

		script.Parent.L4.Material = Enum.Material.Glass
		script.Parent.L4.Transparency = 0.5

	end

	if value > 40 and value < 50 then

		script.Parent.L5.Material = Enum.Material.Neon
		script.Parent.L5.Transparency = 0

	else

		script.Parent.L5.Material = Enum.Material.Glass
		script.Parent.L5.Transparency = 0.5

	end
	
	game:GetService("RunService").RenderStepped:Wait()

end

More context, please. Where is this script located? I’m just trying to get a general understanding of what you want fixed.

1 Like

Its in a tool. Right now its only effecting one part in the folder in workspace. I want it to effect all parts.

Okay. I’m going to mess around with this for a bit. I’ll get back to you if I ever come across a solution!

1 Like

Your code is outside of your pairs loop. It is only running once per step because you will only get the lastly defined “value” variable.

So your saying I should put all the if statements inside the for loop?

Yes, I am saying that. Here is said code:

while true do

	local char = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()
	
	for i,v in pairs(workspace.GhostMagnitudeThingies:GetChildren()) do
			
			distance = (char:WaitForChild("HumanoidRootPart").Position - v.Position).magnitude

			value = math.floor(math.clamp(50 - distance, 0, 50))


	
	if value > 0 and value < 10 then

		script.Parent.L1.Material = Enum.Material.Neon
		script.Parent.L1.Transparency = 0

	else

		script.Parent.L1.Material = Enum.Material.Glass
		script.Parent.L1.Transparency = 0.5

	end

	if value > 10 and value < 20 then

		script.Parent.L1.Material = Enum.Material.Neon
		script.Parent.L1.Transparency = 0

	else

		script.Parent.L2.Material = Enum.Material.Glass
		script.Parent.L2.Transparency = 0.5

	end

	if value > 20 and value < 30 then

		script.Parent.L3.Material = Enum.Material.Neon
		script.Parent.L3.Transparency = 0

	else

		script.Parent.L3.Material = Enum.Material.Glass
		script.Parent.L3.Transparency = 0.5

	end

	if value > 30 and value < 40 then

		script.Parent.L4.Material = Enum.Material.Neon
		script.Parent.L4.Transparency = 0

	else

		script.Parent.L4.Material = Enum.Material.Glass
		script.Parent.L4.Transparency = 0.5

	end

	if value > 40 and value < 50 then

		script.Parent.L5.Material = Enum.Material.Neon
		script.Parent.L5.Transparency = 0

	else

		script.Parent.L5.Material = Enum.Material.Glass
		script.Parent.L5.Transparency = 0.5

	end
        end
	
	game:GetService("RunService").RenderStepped:Wait()

end

It still only works for one thing in the folder. :man_shrugging:

Heres the folder
image

I don’t understand some of your explorer. What exactly is “L1?”

Thats inside the tool L1 is the first lightup thingy.

image

The system works well for me. However, since you only have two parts, assuming that they are quite close to each other, you will only affect L1.

Add me on discord

Dom.#0381

We shall further continue this conversation where we can quickly send messages.