Why is my loop not working correctly?

So basically I want to create this but all the lights light up at some point, but right now only 3 of them work. Why is that?


(For information, it intervals in 3 because that’s how much studs apart the lights are)
This is the print of the chir variable, as you can see it only goes for 5 iterations and then restarts, which only lights up 3 lights
image

Code:

local chir = script.Parent.First.Position.X-3
local sc = false
while wait() do
	print(chir)
	sc = false
	for i,v in pairs(script.Parent:GetChildren()) do
		if v~=script then
			v.Color = Color3.fromRGB(0,0,0)
			v.SurfaceLight.Brightness = 0
		end
	end
	for i,v in pairs(script.Parent:GetChildren()) do
		if v~=script then
			if v.Position.X==chir then
				sc = true
				v.Color = Color3.fromRGB(181, 192, 206)
				v.SurfaceLight.Brightness = .5
			elseif v.Position.X==chir-3 then
				sc = true
				v.Color = Color3.fromRGB(101, 107, 115)
				v.SurfaceLight.Brightness = .25
			elseif v.Position.X==chir+3 then
				sc = true
				v.Color = Color3.fromRGB(101, 107, 115)
				v.SurfaceLight.Brightness = .25
			end
		end
	end
	if sc==false then
		chir = script.Parent.First.Position.X-3
		wait(1)
	else
		chir = chir+3
		wait()
	end
end

What I have right now:

1 Like

For a post like this, showing your workspace would be very helpful :smiley:

1 Like

Not necessarily, though here is my hierarchy for the model.


Just a model with a script and parts inside with a surfacelight. Nothing complicated.

I just want to make sure that the problem doesn’t occur with the object tree :slight_smile:

I believe your problem is right here, though. You are only applying 3 conditions, hence why only 3 of your lights are lighting up.

try GetDescendants instead of GetChildren, i’ve had wonky stuff happen with GetChildren in the past, where it outright wouldn’t instance objects correctly, or at all

1 Like

Nope, doesn’t work.

local chir = script.Parent.First.Position.X-3
local sc = false
while wait() do
	print(chir)
	sc = false
	for i,v in pairs(script.Parent:GetDescendants()) do
		if v~=script then
			if v:IsA("BasePart") then
				v.Color = Color3.fromRGB(0,0,0)
				v.SurfaceLight.Brightness = 0
			end
		end
	end
	for i,v in pairs(script.Parent:GetDescendants()) do
		if v~=script then
			if v:IsA("BasePart") then
				if v.Position.X==chir then
					sc = true
					v.Color = Color3.fromRGB(181, 192, 206)
					v.SurfaceLight.Brightness = .5
				elseif v.Position.X==chir-3 then
					sc = true
					v.Color = Color3.fromRGB(101, 107, 115)
					v.SurfaceLight.Brightness = .25
				elseif v.Position.X==chir+3 then
					sc = true
					v.Color = Color3.fromRGB(101, 107, 115)
					v.SurfaceLight.Brightness = .25
				end
			end
		end
	end
	if sc==false then
		chir = script.Parent.First.Position.X-3
		wait(1)
	else
		chir = chir+3
		wait()
	end
end

Yes, but 3 is being added to chir every iteration of the loop, and chir only resets back to its initial value after there is no part with an X that’s equal to chir or 3 less or more than chir

Can anyone help me please I’m stuck on this problem

I feel like the best thing you should do is loop through every part in the folder and not get a certain position. You can number these parts from 1 to the max number and use your iteration value as the identifier

With your method, it gets really confusing especially when dealing with specific positions

Hell naw, I won’t number my parts, it’s going to take eternity. Besides, it’s not robust either, if I want to create a copy of this, I’ll have to position them all one by one, or just create new ones and renumber them.

1 Like

@Beastcraft_Gaming You most likely read this post.

@iSyriux Adding onto Beastcraft_Gaming, you should use RunService.Stepped instead of wait().

1 Like

That still doesn’t fix the problem. I’m not looking for better ways to loop my code.

If anyone is able to help me with this that would be terrific

You do realize that you have an executable command bar in Studio right? You could run code through studio without going through every individual block

What does that have to do with my sitaution

If you don’t want to number your parts, you can do it via script

That still doesn’t fix my problem though. I want a fix for my script, not a cheaty solution

Damn, 3 days and still no one has come up with even the slightest avaval solution to my anaswer

Well, as always, I found the solution myself without any help…


Turns out, the X increments were off by a few millionths, which caused them to not register. I fixed it by using math.ceil.

1 Like