Choose Random Color in a Table

Hi, I want to make a function that change the color of all platforms with a random color in the “ColorList”. This is the table:

local ColorModule = {}
ColorModule.ColorList = {
	["Purple"] = {
		["Value"] = BrickColor.new(81, 39, 154)
	},
	["White"] = {
		["Value"] = BrickColor.new(227, 227, 227)
	},
	["Dark Red"] = {
		["Value"] = BrickColor.new(107, 5, 5)
	},
	["Cyan"] = {
		["Value"] = BrickColor.new(107, 5, 5)
	},
	["Light Green"] = {
		["Value"] = BrickColor.new(45, 157, 43)
	}
}

Why is the output shows that the “Value” is index nil?
image


This is the “RefreshColor” function:

local ColorModule = require(script.ColorModule)

function PlatformsModule.RefreshColor()
	for _, platform in pairs(Platforms:GetChildren()) do
		local randomObject = Random.new()
		local chosenColor = ColorModule[randomObject:NextInteger(1, #ColorModule.ColorList)].Value
		platform.BrickColor = chosenColor
	end
end

Do this:

local ColorModule = require(script.ColorModule)
local colorsRandomTable = {"Purple", "White", "Dark Red", "Cyan", "Light Green"} -- Add colors when you need to
function PlatformsModule.RefreshColor()
	for _, platform in pairs(Platforms:GetChildren()) do
		local randomIndex = math.random(1, #colorsRandomTable)
        local randomColor = colorsRandomTable[randomIndex]
          
        platform.BrickColor = ColorModule.ColorList[randomColor]["Value"]
	end
end

Might work:

Colors ={
[1] = Color3.fromRGB(255,255,255) -- White
[2] = Color3.fromRGB(0,0,0) -- Black
}

local RC = Colors[math.random(1,#Colors)]
script.Parent.Color = Colors[RC]

Sorry, i mispelled RGB with RBG

They will not be able to expand the dictionary if they want to add more attributes to a color. Also, a naming convention is one of the things a dictionary is for. There will also be a problem in recognizing which color is which since you will need to add comments in each one.

Simple fix:

Colors ={
[1] = Color3.fromRGB(255,255,255); -- White
[2] = Color3.fromRGB(0,0,0); -- Black
}

local RC = Colors[math.random(1,#Colors)]
script.Parent.Color = Colors[RC]

Thank you LOL

Edit: They can add custom colors if they like, doesnt really matter

They can also do this:

Colors ={
[1] = BrickColor.new("Indurstrial white"); -- White
[2] = BrickColor.new("Really black"); -- Black
}

local RC = Colors[math.random(1,#Colors)]
script.Parent.BrickColor = Colors[RC]

Full script should be like this:

local ColorModule = {}
ColorModule.ColorList = {
	["Purple"] = {
		["Value"] = BrickColor.new(81, 39, 154)
	},
	["White"] = {
		["Value"] = BrickColor.new(227, 227, 227)
	},
	["Dark Red"] = {
		["Value"] = BrickColor.new(107, 5, 5)
	},
	["Cyan"] = {
		["Value"] = BrickColor.new(107, 5, 5)
	},
	["Light Green"] = {
		["Value"] = BrickColor.new(45, 157, 43)
	}
}
local ColorModule = require(script.ColorModule)
local colorsRandomTable = {"Purple", "White", "Dark Red", "Cyan", "Light Green"} -- Add colors when you need to
function PlatformsModule.RefreshColor()
	for _, platform in pairs(Platforms:GetChildren()) do
		local randomIndex = math.random(1, #colorsRandomTable)
        local randomColor = colorsRandomTable[randomIndex]
          
        platform.BrickColor = ColorModule.ColorList[randomColor]["Value"]
	end
end

Also @DasKairo your welcome !

I changed my ColorList Table to this:

ColorModule.ColorList = {
	[1] = {
		["Value"] = Color3.fromRGB(81, 39, 154),
		["Name"] = "Purple"
	},
	[2] = {
		["Value"] = Color3.fromRGB(227, 227, 227),
		["Name"] = "White"
	},
	[3] = {
		["Value"] = Color3.fromRGB(255, 43, 43),
		["Name"] = "Light Red"
	},
	[4] = {
		["Value"] = Color3.fromRGB(107, 5, 5),
		["Name"] = "Cyan"
	},
	[5] = {
		["Value"] = Color3.fromRGB(45, 157, 43),
		["Name"] = "Light Green"
	},
	[6] = {
		["Value"] = Color3.fromRGB(170, 85, 0),
		["Name"] = "Orange"
	}
}

And the function do something like this:

		local randomObject = Random.new()
		local chosenColor = ColorModule.ColorList[randomObject:NextInteger(1, #ColorModule.ColorList)].Value
		platform.Color = chosenColor
1 Like

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