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

How do I correct this script? {UNANSWERED} [PLEASE ANSWER]

I wanted to put only one script for my frame that changes color for each brick. There is like at least 48 parts. This is what I did so far. When I hit play solo, it only function the first part and not the rest. This is the script:

Script:

local red = game.Workspace.Frame4.Part1,Part4,Part7,Part10,Part13

while true do
	red.BrickColor = BrickColor.White()
	wait(0.1)
	red.BrickColor = BrickColor.Green()
	wait(0.1)
	red.BrickColor = BrickColor.Red()
	wait(0.1)
end

Please help me. I am still learning :)

1 answer

✓ Accepted answer

Using commas in that way makes a tuple. red will only be given the first thing in the tuple.

This is not an appropriate case for a tuple.


It doesn't really make sense for a variable to simultaneously "be" all of those things (what would that mean? What do you do if you want to do math on such a thing? How do you separate the values later?)


You likely want a list of objects:

local frame = workspace.Frame4
local parts = { frame.Part1, frame.Part4, frame.Part7, Frame.Part10, Frame.Part13 }
-- Curly braces. An entirely different thing.

parts is now a list. Changing parts (the list) won't mean anything -- a list doesn't have a color, for instance.

If you want to change all of their colors, that means you change the color of each, using a loop:

function changeColor( list, color)
	for _, element in pairs(list) do
		element.BrickColor = color
	end	
end

while true do
	changeColors( parts, BrickColor.White() )
	wait(0.1)
	changeColors( parts, BrickColor.Green() )
	wait(0.1)
	changeColors( parts, BrickColor.Red() )
	wait(0.1)
end

Ideally you would not list all of the parts in the definition of parts and instead would group them into a model. Then you could simply say parts = frame.Model:GetChildren().

1 comment

0

Thank you so much! I wanted to know why it always did it on the first one. Thanks for your explanation for this. :D

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