I need assistance at scripting!

So I’ve been having a trouble with writing a code. Simply it will going to give a color to every part inside a model depends on a attribute in model which is called “BulbType”. However, only 1 of the parts inside the model gets colored and other parts doesn’t gets colored. Have I done anything wrong?

local Tag = "Bulb"

local CollectionService = game:GetService("CollectionService")

function getBulb()
	for _, bulb in CollectionService:GetTagged(Tag) do
		if not bulb then return end
		return bulb
	end
end

function getBulbParts(bulb)
	for _, bulbParts in bulb:GetDescendants() do
		if bulbParts:IsA("BasePart") then
			return bulbParts
		end
	end
end

function setBulbs()
	local bulb = getBulb()
	if bulb:GetAttribute("BulbType") == "Kill" then
		local bulbParts = getBulbParts(bulb)
		bulbParts.BrickColor = BrickColor.new("Really red")
	elseif bulb:GetAttribute("BulbType") == "Health" then
		local bulbParts = getBulbParts(bulb)
		bulbParts.BrickColor = BrickColor.new("Lime green")
	elseif bulb:GetAttribute("BulbType") == "Push" then
		local bulbParts = getBulbParts(bulb)
		bulbParts.BrickColor = BrickColor.new("Royal purple")
	end
end

setBulbs()
1 Like

I think it’s because using return mid-loop stops it. You can shorten the script, too.

local COLORS = {
	Kill		=	"Really red",
	Health	=	"Lime green",
	Push	=	"Royal purple"
}

for _, bulb in CollectionService:GetTagged("Bulb") do
	for _, bulbPart in bulb:GetDescendants() do
		local bulbColor = COLORS[bulb:GetAttribute("BulbType")]
		if bulbPart:IsA("BasePart") then
			bulbPart.BrickColor = bulbColor
		end
	end
end

Also just asking, is there a difference between using in pairs() and simply using in? I usually see people using pairs() to loop through instances and tables.

I don’t think they are different.

Worked but you forgot to define BrickColor.new(“BrickColor”) at COLOTS table.

1 Like

Oops, right, I wanted to use the string on BrickColor.new().

bulbPart.BrickColor = BrickColor.new(bulbColor)

It doesn’t really change much but that’s just me

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