For loop doesn't apply for all parts

Hello! I am trying to make a script in which i change multiple brick colors of multiple parts that all contain a tag. This seems pretty simple to me but for some reason which i cannot pin-point, the loop does not change any brickcolors but does still run as the print statements still run as usual. This confuses me a lot, why does the brickcolor not change anymore but the print statements do run? please help if you know

code (inside of a local script in startercharacterscripts):


for _,v in pairs(game:GetService("CollectionService"):GetTagged("jimbob"))  do
	--	local Beam = v:WaitForChild("Decal")
	task.spawn(function()
		while wait() do
			v.BrickColor=BrickColor.new("Really red")
			print(0)
			task.wait(2)
			v.BrickColor=BrickColor.new("Cyan")
			print(1)
			task.wait(2)
		end
	end)
end
1 Like

You might want to check if v is a BasePart before continuing, let me know if this doesn’t fix the problem.

1 Like

i have tried this, it has not worked, it still does the same thing as before, no difference

It would be better if you do it like so:

local CollectionService = game:GetService("CollectionService")
while true do
	for _,v in CollectionService:GetTagged("jimbob")  do
		v.BrickColor=BrickColor.new("Really red")
	end
	task.wait(2)
	for _,v in CollectionService:GetTagged("jimbob")  do
		v.BrickColor=BrickColor.new("Cyan")
	end
	task.wait(2)
end
3 Likes

These are what’ve I thought of

  1. Try changing the script to a server script in SSS
  2. Try modifying the color3 instead of the brickcolor

wow! This actually ended up working perfectly with just a copy and paste, thank you! I’m curious though, what is the big difference which made this one work compared to the last one? once again, thank you!

Mainly just the fact you used a while loop in a for loop which is bad practice because it would create too many while loops. The other is just roblox…

Andddd no problem. : )

2 Likes

ahh i see, so the while loop has to be outside of the for loop, that actually makes a lot of sense, i hadn’t thought of that. and it also being roblox is also nice to know :') thank you so much this was a great thing to learn and i was able to fix my problem with your help, thank you and have a good day/night/ anythign!!

Yep, no worries. You too have a great day/night. Bye!

2 Likes

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