Math.random returns the same exact value until player leaves

  1. What do you want to achieve? A random color picker that chooses one color from a table out of 4

  2. What is the issue? Math.random returns the same color until player leaves

  3. What solutions have you tried so far? i tried using random.new and it didnt work.

Here is my random color picker script:

local colors = {
	Color3.fromRGB(255, 255, 255); --white
	Color3.fromRGB(255, 94, 239); -- pink
	Color3.fromRGB(209, 203, 255); -- pastel
	Color3.fromRGB(170, 255, 255); -- cyan
}
local color = colors[math.random(1, #colors)]
print(color) --print is not main code just for demonstration 

Script is a module script found in serverstorage.

Does the script give any error in console?

The one you posted yes, it does, here it is:
Unable to assign property Color. Color3 expected, got number
here is the line that caused the error
L.Color = color

This is because you are doing

L.Color = color

You have to do this:

L.Color = Color3.fromRGB(color)

And then you can just make the color list like this:

local colors = {
	"255, 255, 255"; --white
	"255, 94, 239"; -- pink
	"209, 203, 255"; -- pastel
	"170, 255, 255"; -- cyan
}

It still returns the same exact value, but this time it’s red
image
here is the output and the script im currently using `local colors = {
“255, 255, 255”; --white
“255, 94, 239”; – pink
“209, 203, 255”; – pastel
“170, 255, 255”; – cyan
}
local color = math.random(1, #colors)

print(color)
L.Color = Color3.new(color)

`

Is the color variable being used multiple times or are you making a new one for each time you want to pick the color?

1 Like

new one each time i want to pick the color, what i’m basically trying to do is pick a random color out of for 4, then print that one color.

It’s already a Color3, he is doing it correctly. The problem is that he only gets a random color once.

Yes, true and i only get a new color if i rejoin the game.

Every time you need a new random color you need to run this line again

It’s hard to diagnose because all we have right now is a working snippet of code to look at.

How would i exactly approach in doing that?

The whole point of the module script is to color an instance.new part depending on the random color

Try this:

local colors = {
	Color3.fromRGB(255, 255, 255); --white
	Color3.fromRGB(255, 94, 239); -- pink
	Color3.fromRGB(209, 203, 255); -- pastel
	Color3.fromRGB(170, 255, 255); -- cyan
}
local index = math.floor(Random.new():NextNumber(1, #colors))
local color = colors[index];
print(index)
print(color) --print is not main code just for demonstration 

Yeah I get that but we can’t really fix code we can’t even see.
Leave out everything unrelated to the color and send the script if you can.

The color not changing until the module is run again when the player rejoins would literally mean the variable isn’t being updated even if you’re saying it is.

Here is the entire script

	Color3.fromRGB(255, 255, 255); --white
	Color3.fromRGB(255, 94, 239); -- pink
	Color3.fromRGB(209, 203, 255); -- pastel
	Color3.fromRGB(170, 255, 255); -- cyan
}
local index = Random.new():NextNumber(1, #colors)
local color = colors[index];
function createPart(i, dontTween)
	local SpecialMesh
	local L = Instance.new("Part")

	local Weld = Instance.new("Weld")
	Weld.C0 = dontTween and CFrame.new() or CFrame.new(0, -(i.Size.Y/2), 0)
	Weld.Part0 = i
	Weld.Part1 = L
	Weld.Parent = L
	if i:FindFirstChildWhichIsA("SpecialMesh") then
		local CSM = i:FindFirstChildWhichIsA("SpecialMesh"):Clone()
		CSM.Scale = CSM.Scale
		CSM.TextureId = ""
		CSM.Parent = L
	else
		SpecialMesh = Instance.new("SpecialMesh")
		SpecialMesh.MeshId = "rbxasset://fonts/torso.mesh"
		SpecialMesh.MeshType = Enum.MeshType.FileMesh
		SpecialMesh.Parent = L
		SpecialMesh.Scale = dontTween and Vector3.new(i.Size.X/1.515, i.Size.Y/1.515, i.Size.Z/1.515) or Vector3.new(i.Size.X/1.515, 1.515, i.Size.Z/1.515)
	end
	L.CanCollide = false
	L.CanTouch = false
	L.CanQuery = false
	L.CFrame = i.CFrame
	L.Size = i.Size
	L.Shape = i.Shape
	L.Material = Enum.Material.SmoothPlastic
	print(color)
	L.Color = Color3.new(color)```

Put the local color = inside the function it should fix it.
With the index part too of course.

1 Like

Now it just gives a black color instead of the four, script says “invalid argument to color3 constructor”

cuz it’s an HVS value probably

Use math.floor() around the index (my bad)
here:

math.floor(Random.new():NextNumber(1, #colors))