#I'm having some problems where the math.random string is not calling a random color.

24 messages · Page 1 of 1 (latest)

velvet saffron
#

-- Module Script

local ColorTable = {
    ["Red"] = {
        Color = BrickColor.new("Bright red"),
        Name = "Red"
    };
    ["Orange"] = {
        Color = BrickColor.new("Bright orange"),
        Name = "Orange"
    };
    ["Yellow"] = {
        Color = BrickColor.new("Bright yellow"),
        Name = "Yellow"
    };
    ["Green"] = {
        Color = BrickColor.new("Bright green"),
        Name = "Green"
    };
    ["Dark Green"] = {
        Color = BrickColor.new("Earth green"),
        Name = "Dark Green"
    };
    ["Teal"] = {
        Color = BrickColor.new("Teal"),
        Name = "Teal"
    };
    ["Blue"] = {
        Color = BrickColor.new("Bright blue"),
        Name = "Bright blue"
    };
    ["Purple"] = {
        Color = BrickColor.new("Lilac"),
        Name = "Purple"
    };
    ["Pink"] = {
        Color = BrickColor.new("Pink"),
        Name = "Pink"
    };
    ["Brown"] = {
        Color = BrickColor.new("Brown"),
        Name = "Brown"
    };
    ["White"] = {
        Color = BrickColor.new("White"),
        Name = "White"
    };
    ["Black"] = {
        Color = BrickColor.new("Black"),
        Name = "Black"
    };
}
return ColorTable

-- Script

local function NewColor()
    for i, v in ipairs(Floors:GetChildren()) do
        if v:IsA("Part") then
            v.BrickColor = BColors[math.random(1,BColors).Color
        end
    end
end
#

I'm having some problems where the math.random string is not calling a random color.

green nexus
#

but i think it wouldn't work as the indexes in your table are strings

velvet saffron
green nexus
#

the same one ?

velvet saffron
#

Let me rerun the script

velvet saffron
#

or only math.random(1, #BColors)?

velvet saffron
#

Okay

green nexus
#

i just checked and the # doesn't count string indexes

velvet saffron
#

Here is the error

velvet saffron
#

Or with a different symbol?

green nexus
#

you would do something like this

local keys = {}

for key, _ in pairs(BColors) do
    table.insert(keys, key)
end

local function get_random_color()
    return BColors[keys[math.random(1, #keys)]].Color
end
velvet saffron
green nexus
#

in the script itself

velvet saffron
#

okay

#

I'll try that

green nexus
#

that's not the exact code you should use obviously

#

that's just the code you need to get a random value from the table it won't change your part colors

velvet saffron
#

It works!

#

thank you!