Try to use Math.Random to generate Different Color rgb balue

I have recently had an idea to allow players change the color of their name tag for fun. So I made this simple code yet problematic. it wont print anything as I asked it to. This code is still in progress so its not fully complete. I’m still noob in scripting tho and was rushing to finish it.

However, I want the color to not generate this specific rgb values which is the gold color that I put specially for me (the owner). This game is just a lobby for my group.

The error is… there is no error or output result. so I’m stuck…

This code is my attempt to generate the 3 rgb value and I put it in the ServerScriptService

game.ReplicatedStorage.ColorChanger.OnServerEvent:Connect(function(player)
	local Gold1 = 255
	local Gold2 = 215
	local Gold3 = 0
		local C1 = math.random(000,255)
		local C2 = math.random(000,255)
		local C3 = math.random(000,255)
		while true do
			if C1 == Gold1 then
				C1 = math.random(000,255)
				if C2 == Gold2 then
					C2 = math.random(000,255)
					if C3 == Gold3 then
						C3 = math.random(000,255)
					else
						return C3;
					end
				else
					return C2;
				end
			else
				return C1;
		end
		
		print("Player get:" .. C1..C2..C3)
		end

end)

This is the LocalScript under the Textbutton

script.Parent.MouseButton1Click:Connect(function()
	game.ReplicatedStorage.ColorChanger:FireServer()
end)
3 Likes

This is a great idea! Try this:

game.ReplicatedStorage.ColorChanger.OnServerEvent:Connect(function(player)
    local R = math.random(0, 255)
    local G = math.random(0, 255)
    local B = math.random(0, 255)
    
    local colorValue = Instance.new("Color3Value")
    colorValue.Value = Color3.fromRGB(R, G, B)
    colorValue.Name = "Color"
    colorValue.Parent = game:GetService("ServerScriptService")
end)
3 Likes

I actually already have this outside of the code I showed. just too long to paste everything here cause seem unnecessary. but what im trying to achieve is to check if the rgb value of each math.random() does not generate rgb values of gold color and print it so i check if it works. Can you just improve my already made code? thank you in advanced.

1 Like

Ok try this method:

game.ReplicatedStorage.ColorChanger.OnServerEvent:Connect(function(player)
    local blacklistedColorBytes = {255, 215, 0}
    local newColorSelectionBytes = {}

    for i=0, 255 do
        if table.find(blacklistedColorBytes, i) then continue end
        table.insert(newColorSelectionBytes, i)
    end

    local R = newColorSelectionBytes[math.random(1, #newColorSelectionBytes)]
    local G = newColorSelectionBytes[math.random(1, #newColorSelectionBytes)]
    local B = newColorSelectionBytes[math.random(1, #newColorSelectionBytes)]
    
    local colorValue = Instance.new("Color3Value")
    colorValue.Value = Color3.fromRGB(R, G, B)
    colorValue.Name = "Color"
    colorValue.Parent = game:GetService("ServerScriptService")
end)
3 Likes

Nice job @Moonvane making the OP’s spec, however I don’t see how this will accomplish much.

OP, have you considered anything ‘near’ gold is gonna look gold. (254,216,1) for example will look identical to your gold.

You may be better off making an array of approved colors and then randomizing that.

1 Like

Thats true, and thats probably a better idea actually

1 Like

You could use the brickcolor codes to easily build your approved array of colors:

1 Like

True, I did thought about that but I just want to see whats wrong with my code. Really spend my time doing research about it and not seeing it to work and use totally different script kinda sad :frowning:

This looks 100% better than my code lol. will try it. but i really want to see the code of mine to work lol.

Okay, so for your script, the reason you aren’t seeing anything printed is because when your function hits the first Return, you are done, function over. You shouldn’t have more than one return like that unless you want variable single things to be returned.

1 Like

Also, to help with your troubleshooting skills, I’d recommend putting a print statement at the beginning of the function before any loops or if statements. like print(“server color script fired”) then another in the while loop print(“in while loop…”) you get the picture. When you get no output, make it give you some output so you can see where its not doing what you expect.

1 Like

Oh… ok that made sense now haha. my bad. but after trying @Moonvane 's code, its better. thanks guys!

@Moonvane Why are you initalising tables inside the function scope if it’s not going to be modified afterwards? Why not out the function scope?
Why are you creating a Color3Value every time it changes?
Also Lua conventally start tables at 1 (not 0) so why are your math.random argument start at 0? If it did manage to get 0, it’ll return nil.

Also use table.create to initalise an array-like tables in Lua (table resizing is expensive in Lua(u)).

Here’s a more efficient one

local blacklistedColorBytes = {[255] = true, [215] = true, [0] = true}
local newColorSelectionBytes = table.create(256)
local colorSelectionLength = 0
for i = 0, 255 do
    if not blacklistedColorBytes[i] then
        colorSelectionLength += 1
        newColorSelectionBytes[colorSelectionLength] = i
    end
end

game.ReplicatedStorage.ColorChanger.OnServerEvent:Connect(function(player)
    local r = newColorSelectionBytes[math.random(1, colorSelectionLength)]
    local g = newColorSelectionBytes[math.random(1, colorSelectionLength)]
    local b = newColorSelectionBytes[math.random(1, colorSelectionLength)]
    print(r, g, b)
end
1 Like

Why not? Because he just needed help with a simple problem and I didn’t even think of efficiency yet. Your code looks good though

You’re not breaking out of that loop correctly. You’re returning the function not exciting the loop.
To Exit a loop you do break to exit a function return to go to the next entry in a loop continue