Color assigning

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    Every time a part joins a folder, it will receive a highlight with a color. However, there can’t be more than one of the same color. So, the part will receive a different color that hasn’t been taken yet.

  2. What is the issue? Include screenshots / videos if possible!
    There are more than one of the same color

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I tried looking through the documentations that Roblox and the devforum to see how I can use repeat until, but I couldn’t figure out how to use it. I also tried to use for loops but I’m not the best at it.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

local folder = script.Parent

local highlightColors = {
	Color3.fromRGB(255, 70, 70),	-- Red
	Color3.fromRGB(255, 110, 70),	-- Orange
	Color3.fromRGB(255, 200, 70),	-- Yellow
	Color3.fromRGB(70, 255, 70),	-- LightGreen
	Color3.fromRGB(70, 70, 255),	-- Blue
	Color3.fromRGB(70, 225, 255),	-- LightBlue
	Color3.fromRGB(125, 70, 255),	-- Purple
	Color3.fromRGB(255, 70, 255)	-- Pink
}

local usedColors = {}

local function assignHighlightColor(part)
	local randomChance = math.random(1, #highlightColors)
	local randomColor = highlightColors[randomChance]
	local highlight = Instance.new("Highlight", part)
	
	if randomColor == usedColors then
		assignHighlightColor(part)
		table.insert(usedColors, randomColor)
	else
		highlight.FillColor = randomColor
		highlight.OutlineColor = randomColor
		usedColors[randomColor] = true
	end
end

folder.ChildAdded:Connect(function(addedPart)
	assignHighlightColor(addedPart)
end)

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

1 Like

To answer your question exactly;
Remove the colour chosen from the table, with the way youve structured your table, you can do it like this: table.remove(hightlightColors, table.find(hightlightColors, randomColors)).
You can remove the usedColors table and code relating to it.

If you wish to not remove colours from your table, try this:

local UnusedColors = highlightColors

-- your function code
local randomColor = UnusedColors[math.random(1, #UnusedColors)])

--highlight code
table.remove(UnusedColors, table.find(UnusedColors, randomColor))

Although your code raises some concerns;
Roblox has a limit to how many highlights you can have at once (31).
This isnt sustainable if you plan of having that many players. I would recommend looking into other designs.

2 Likes

Im guessing the issue is on this line here, could be wrong though…

if randomColor == usedColors then

Shouldn’t you use table.find() ?

Thank you for the help! I wouldn’t have thought of that but now I know. The more you know I suppose.

Additionally, I don’t plan on adding more than 31 highlights, but I do appreciate the heads up because I didn’t know highlights had a limit. I plan on trying to only have 4 highlights but have 8 colors to add more variety.

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