Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How would I select a specific database table {}?

Hello,

So I would like to select the specific BrickColorVal's value in the table, I however, just have no idea how. I tried using stringmatch, but it ends up spacing out the letters. Like instead of "Medium stone grey" it spaces out to "M e d i u m s t o n e g re y" And I don't want them to space out.

    if name=="Color" then --Getting the table that is titled "Color" 

            local components = {}
                for num in string.gmatch(cf, ".") do
                components[#components+1] = tostring(num)
            end
            print(unpack(components)) --Prints "M e d i u m s t o n e g r e y"
            model.BrickColorVal.Value=BrickColor.new(unpack(components))
        end

So how would I select the table in a professional and atlernative way? Thank you!

1 answer

✓ Accepted answer

From what I can gather, cf is the name of the BrickColor, and if that's the case you're trying to convert the string to a table and back to the same table for no apparent reason.

Since BrickColor.new() can take a string corresponding to the color, you can directly feed it cf and get the BrickColor you want:

if name=="Color" then
    color = BrickColor.new(cf)
    model.BrickColorVal.Value = color
end

Putting aside the practicality of the solution, you seem to have misunderstood how unpack() works. It doesn't join elements of the table into a single string but returns a tuple or several elements separately, not joined; similar to returning three variables as return a, b, c.

Instead you should be using the table.concat(tab, sep) method, which allows you to concatenate the elements of the table tab into a single string with the separator sep (defaults to an empty string, so there's no spaces in between). Here's an example of its usage:

local characters = {"He", "ll", "o ", "Wo", "rld", "!"}

local combined = table.concat(characters) -- "" is the default separator
print(combined) --> "Hello World!"

What was happening before was you were doing something like:

local characters = {"He", "ll", "o ", "Wo", "rld", "!"}

print(characters[1], characters[2], characters[3], ...)

Since each value is separated, the resultant output has spaces between each value.

This discussion is preserved as it was. New answers, comments, and votes are closed.