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?
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.
Try removing the underscores.