Math.random keeps picking the same color

Hello, Developers!
I’m trying to get some code to pick a random color from a table for terrain everytime the server starts up.

But everytime I start up the server, it always lands on red.

Here’s the server code

local colorSelection = {
	"108, 141, 71"; -- Green
	"255, 119, 0"; -- Orange
	"255, 191, 0"; -- Yellow
	"119, 0, 255"; -- Purple
	"171, 0, 0"; -- Red
	"84, 167, 39"; -- Lime green
}

local function getRandomColor()
	local grassColor = Color3.new(math.random(1,#colorSelection))
	game.Workspace.Terrain:SetMaterialColor(Enum.Material.Grass, grassColor)
	game.Workspace.Terrain:SetMaterialColor(Enum.Material.Ground, grassColor)
	return grassColor
end

task.wait(2)
getRandomColor()


You separate tables with comas: , and I think should set the values to Color3s

local colorSelection = {
	Color3.fromRGB(108, 141, 71), -- Green
	Color3.fromRGB(255, 119, 0), -- Orange
	Color3.fromRGB(255, 191, 0), -- Yellow
	Color3.fromRGB(119, 0, 255), -- Purple
	Color3.fromRGB(171, 0, 0), -- Red
	Color3.fromRGB(84, 167, 39), -- Lime green
}

Try that.

1 Like

try this:

local function GRGB() -- Get Red Green Blue lol
local Random = colorSelection[math.random(1,#colorSelection)]

game.Workspace.Terrain:SetMaterialColor(Enum.Material.Grass, Color3.fromRGB(tonumber(colorSelection[Random])))
game.Workspace.Terrain:SetMaterialColor(Enum.Material.Ground, Color3.fromRGB(tonumber(colorSelection[Random])))
end

This doesnt matter, both ; and , work on tables

2 Likes

Try this:

local colorSelection = {
	"108, 141, 71"; -- Green
	"255, 119, 0"; -- Orange
	"255, 191, 0"; -- Yellow
	"119, 0, 255"; -- Purple
	"171, 0, 0"; -- Red
	"84, 167, 39"; -- Lime green
}

local function getRandomColor()
	local grassColor = Color3.new(colorSelection[math.random(1,#colorSelection)])
	game.Workspace.Terrain:SetMaterialColor(Enum.Material.Grass, grassColor)
	game.Workspace.Terrain:SetMaterialColor(Enum.Material.Ground, grassColor)
	return grassColor
end

task.wait(2)
getRandomColor()

Likes when @DasKairo says, u need to do this: table[math.random(1, #table)]

I never knew that, every day you learn!

The terrain showed up completely black?

can you send the game output? maybe that is an error


No errors.

image

This is because putting a string for Color3 constructor will just use tonumber on it, make a list of Color3’s and choose from that.

Examples:

Color3.FromRGB("255,255,255") = Color3.FromRGB(255)
1 Like

Okay, I changed it to that but it seems that it’s changing it to red again.

I fixed my script, try this:

local colorSelection = {

Color3.new(0,0,0);

Color3.new(255,255,255);

}

local function GRGB() -- Get Red Green Blue lol

local Randoms = colorSelection[math.random(1,#colorSelection)]

print(Randoms)

game.Workspace.Terrain:SetMaterialColor(Enum.Material.Grass, Randoms)

game.Workspace.Terrain:SetMaterialColor(Enum.Material.Ground, Randoms)

end

task.wait(1)

GRGB()


Use Color3.new() as Color3.fromRBG() doesnt work for some reason

What does the code look like now?

Try adding math.randomseed(tick()) to the top of your script.

Color3.new() is on a range of 0 - 1, so either use Color3.fromRGB, or move it to a range of 0 - 1.


local colorSelection = {
	Color3.fromRGB(108, 141, 71), -- Green
	Color3.fromRGB(255, 119, 0), -- Orange
	Color3.fromRGB(255, 191, 0), -- Yellow
	Color3.fromRGB(119, 0, 255), -- Purple
	Color3.fromRGB(171, 0, 0), -- Red
	Color3.fromRGB(84, 167, 39), -- Lime green
}

local function getRandomColor()
	local grassColor = Color3.new(colorSelection[math.random(1,#colorSelection)])
	math.randomseed(0)
	game.Workspace.Terrain:SetMaterialColor(Enum.Material.Grass, grassColor)
	game.Workspace.Terrain:SetMaterialColor(Enum.Material.Ground, grassColor)
	return grassColor
end

task.wait(2)
getRandomColor()

Don’t Color3.new(Color3.fromRGB()), this doesn’t make sense and doesn’t work.

local grassColor = colorSelection[math.random(1,#colorSelection)]

I test my code before i post it, looks like it works for me:

I added a Loop to test it and yes, it works

Output from looping:

11:51:43.370   ▶ 0, 0, 0 (x2)  -  Server - Script:10
  11:51:45.505   ▶ 255, 255, 255 (x2)  -  Server - Script:10
  11:51:47.537  0, 0, 0  -  Server - Script:10
  11:51:48.553  255, 255, 255  -  Server - Script:10
  11:51:49.555  0, 0, 0  -  Server - Script:10
  11:51:50.571   ▶ 255, 255, 255 (x3)  -  Server - Script:10
  11:51:53.604   ▶ 0, 0, 0 (x8)  -  Server - Script:10
  11:52:01.654   ▶ 255, 255, 255 (x2)  -  Server - Script:10
  11:52:03.686  0, 0, 0  -  Server - Script:10
  11:52:04.687  255, 255, 255  -  Server - Script:10
  11:52:05.703   ▶ 0, 0, 0 (x4)  -  Server - Script:10
  11:52:09.770   ▶ 255, 255, 255 (x4)  -  Server - Script:10
  11:52:13.820   ▶ 0, 0, 0 (x3)  -  Server - Script:10
  11:52:16.853   ▶ 255, 255, 255 (x2)  -  Server - Script:10
  11:52:18.887  0, 0, 0  -  Server - Script:10
  11:52:19.903  255, 255, 255  -  Server - Script:10
  11:52:20.919  0, 0, 0  -  Server - Script:10
  11:52:21.937  255, 255, 255  -  Server - Script:10
  11:52:22.953  0, 0, 0  -  Server - Script:10
  11:52:23.969   ▶ 255, 255, 255 (x2)  -  Server - Script:10
  11:52:25.985  0, 0, 0  -  Server - Script:10
  11:52:27.003  255, 255, 255  -  Server - Script:10
  11:52:28.018  0, 0, 0  -  Server - Script:10
  11:52:29.035  255, 255, 255  -  Server - Script:10
  11:52:30.036  0, 0, 0  -  Server - Script:10
  11:52:31.036   ▶ 255, 255, 255 (x2)  -  Server - Script:10
  11:52:33.068  0, 0, 0  -  Server - Script:10
  11:52:34.085   ▶ 255, 255, 255 (x4)  -  Server - Script:10
  11:52:38.135   ▶ 0, 0, 0 (x2)  -  Server - Script:10
  11:52:40.136  255, 255, 255  -  Server - Script:10
1 Like

This works because Color3.new() uses math.Clamp like:

math.Clamp(255, 0, 1) --> 1