Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Help with for i,v in pairs?

I'm trying to get the brick colors to change all at once to 'Bright red'. I'm having trouble because it's doing them one at a time. (Not all at the same time.) Keep in note, this is only the ending part of my script.

if contraband == true and enabled == false then
	enabled = true
		for i,v in pairs(script.Parent.Parent:GetChildren()) do
		if v.Name == 'Light' then
		v.BrickColor = BrickColor.new('Bright red')
		script.Parent.Sound:Play()
		wait(3)
		for i,v in pairs(script.Parent.Parent:GetChildren()) do
			if v.Name == 'Light' then
				v.BrickColor = BrickColor.new('Mid gray')
			end
		end
	enabled = false
	end		
end
end

1 answer

You have a wait inside of your loop.

This means it will:

  • change first part to red
  • wait
  • change second part to red
  • wait

Etcetera.

Put the wait after the loop is done.


In addition, you shouldn't have the second loop (lines 08 to 12 in your snippet) inside of the first:

  • You are reusing the same variable names which already suggests something is wrong
  • This will make this significantly slower and
  • It's more confusing (it is currently behaving wrong -- the way you have it written is not what you meant)

Instead, have the loop after the first one.


It is also very valuable to properly tab your code

Finally, you don't need == false or == true in your conditions. It makes them needlessly verbose. Just use the value, or not with the value.


if contraband and not enabled then
	enabled = true
	for i,v in pairs(script.Parent.Parent:GetChildren()) do
		if v.Name == 'Light' then
			v.BrickColor = BrickColor.new('Bright red')
		end
	end
	script.Parent.Sound:Play()
	wait(3);
	for i,v in pairs(script.Parent.Parent:GetChildren()) do
		if v.Name == 'Light' then
			v.BrickColor = BrickColor.new('Mid gray')
		end
	end
	script.Parent.Sound:Play()
	enabled = false
end

This discussion is preserved as it was. New answers, comments, and votes are closed.