Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
-1

Not printing a Color3 Value from Module?

Asked by
xPolarium 1388 Moderation Voter
9 years ago

I have a module that is supposed to return a Color3 Value from a table of Colors. On another script, it's supposed to print whatever was returned from the function. Below is the module.

local _M = {}

local teamColors = {

    RED_COLOR = BrickColor.new("Really red").Color; --".Color" returns the Color3 value of a BrickColor

    ORANGE_COLOR = BrickColor.new("Deep orange").Color;

    YELLOW_COLOR = BrickColor.new("New yeller").Color;

    GREEN_COLOR = BrickColor.new("Lime green").Color;

    BLUE_COLOR = BrickColor.new("Really blue").Color;

    LIGHT_BLUE_COLOR = BrickColor.new("Toothpaste").Color;

    PURPLE_COLOR = BrickColor.new("Bright violet").Color;

    PINK_COLOR = BrickColor.new("Hot pink").Color;

    BROWN_COLOR = BrickColor.new("Brown").Color;

    BLACK_COLOR = BrickColor.new("Black").Color;

    WHITE_COLOR = BrickColor.new("White").Color;
}

function _M.returnColor(color)
    local moduleColors;
    for i, v in pairs(teamColors) do --Problem is around here?
        return v
    end
end

return _M

I'm really new to Modules and the use of them so everything is very basic here. The output prints (0, 0, 0) which is black. It should print Red first and after that continue until black. It should only print once because "print()" is called when a player clicks a GUI button.

Also, am I going on the right path for this or can I change some things?

0
What do you want `returnColor` to do? Return a random color? Return a list? You can only return one object. What do you need this code for? BlueTaslem 18071 — 9y
0
Your returnColor function is only returning the first colour it iterates onto. What are you looking to do? User#6546 35 — 9y
0
It's supposed to return one color at a time like LetThereBeCode answered with. Sorry for not elaborating this so much. Also, it seems like not replying back gets me a free down. xPolarium 1388 — 9y

2 answers

Log in to vote
1
Answered by 9 years ago

I am not sure what you are trying to do, but my best guess is that you are trying to make it return a one color at a time, iterating through teamColors like a generator function. What you can do is add a variable that keeps track of current color that will be returned. Easiest way to do this is to change your table, if that is not significant.

local _M = {}

local teamColors = {
    BrickColor.new("Really red").Color; --".Color" returns the Color3 value of a BrickColor
    BrickColor.new("Deep orange").Color;
    BrickColor.new("New yeller").Color;
    BrickColor.new("``Lime green").Color;
    BrickColor.new("Really blue").Color;
    BrickColor.new("Toothpaste").Color;
    BrickColor.new("Bright violet").Color;
    BrickColor.new("Hot pink").Color;
    BrickColor.new("Brown").Color;
    BrickColor.new("Black").Color;
    BrickColor.new("White").Color;
}

local currentColor = 0 -- A variable to keep track on which color will be returned next

function _M.returnColor(color)
    currentColor = currentColor + 1 -- Adding 1 so we get next color
    if currentColor >= #teamColors then
        currentColor = 0
    end
    return teamColors[currentColor]
end

return _M

if currentColor > #teamColors then This makes sure we don't return nil, so what I did was, I made it go back to Red after it reached White. I hope this is what you want.

teamColors[currentColor] Here, we are just indexing the table of colors for the appropriate color.

This function, when called should each time return the next color. When it comes to the end of table, it returns to the first color.

I hope this helped.

0
It helps, but i'm wondering why is it still returning (0, 0, 0)? On a separate Script, I'm calling the function with print(Color3.new(module.returnColor). It still returns 0,0,0 though. xPolarium 1388 — 9y
0
What do you want it to return? LetThereBeCode 360 — 9y
Ad
Log in to vote
-4
Answered by 9 years ago

Try removing the underscores.

0
This has absolutely nothing to do with anything. BlueTaslem 18071 — 9y
0
^ 2k rep :O ScriptFusion 210 — 9y

Answer this question