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

Is there a way to check if a string is a BrickColor?

Asked by 5 years ago

So I was making a gui where players can input a BrickColor to change the color of it. But I can't think of any way to check if the input is actually a BrickColor or not, other than slowly entering every BrickColor into a table and checking if it's in that. So I was wondering if there's any way to do this without doing the thing I just mentioned?

0
other than using tables i don't think so. CjayPlyz 643 — 5y
0
i dont think a casual or just any person who has never used studio to script will know what the color names are so you should make them choose from a few colors. like some kind of sub-gui that appears once they click that one to choose a color. yea im still new to scripting so sorry if i didn't exactly answer the question :p dylanthecoolkid11 0 — 5y
0
mk BetterNotDie 11 — 5y
0
you gotta use tables. GlxkTerrence 5 — 5y

3 answers

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

You could use the BrickColor.new function to check if what was inputted was a brick colour, as it'll return Medium stone grey if it wasn't by default. (I thought it still returned nil, but that was apparently changed)

local IsAColourButIsAccepted = BrickColor.new('Really red') -- Is an actual colour, but it's accepted because it's upper cased.
local IsAColourButIsntAccepted = BrickColor.new('really blue') -- Is an actual colour, but it's not accepted because it's not upper cased.

print(IsAColourButIsAccepted) --> Really red
print(IsAColourButIsntAccepted) --> Medium stone grey

Since you're wanting to give it input, you could use the lower, upper, and substring to be able to have the BrickColor.new function accept it, while preventing the case sensitive problem. As an example:

local IsAColour = 'really Red' -- This's an actual colour, but it's not capitalized first and lowercased last.
local BrickColourWithoutPrevention = BrickColor.new(IsAColour)
local BrickColourWithPrevention = BrickColor.new(IsAColour:sub(1, 1):upper() .. IsAColour:sub(2):lower()) -- This upper cases the first letter, and lower cases the rest of IsAColour.

print(BrickColourWithoutPrevention) -- It will return Medium stone grey.
print(BrickColourWithPrevention) -- However, since we now have something set to prevent this problem, it will return `Really red`.

If you have any questions, please let me know. :) Have a nice day! :D

Wait! You're forgetting something!

Oh right...

Stuff that was touched on, but never really explained

  1. BrickColor.new -- A function that'll return a BrickColor, as the name suggests, and will "validate"(?) whether what was given to it was an actual colour, otherwise it'll return Medium stone grey by default.

  2. lower -- Lower cases a string. (local StringThatsUpperCased = 'HELLO WORLD!' print(StringThatsUpperCased:lower()) --> hello world!)

  3. upper -- Upper cases a string. (local StringThatsLowerCased = 'hello world!' print(StringThatsLowerCased:upper()) -- > HELLO WORLD!)

  4. substring -- The best way I can explain it is that it gets the position of where in the string, like in the above examples. (local StringForSubstring = 'Hello World!' print(StringForSubstring:sub(4, 8)) --> lo W)

Ok, now I think I'm done. c: Have a nice day! :D

EDIT

I somewhat misread what was being asked. Oops. ;p

Since BrickColor.new returns Medium stone grey if what was given to it wasn't a colour, you could compare the input to what BrickColor.new returned. An example of this would be:

local InputForColourThatsAccepted = 'Really blue'
local InputForColourThatIsntAccepted = 'really red'

if (BrickColor.new(InputForColourThatsAccepted)).Name == InputForColourThatsAccepted then
    print('What was given was a brick colour!') -- Should print, as it's a valid brick colour.
end

if (BrickColor.new(InputForColourThatIsntAccepted)).Name == InputForColourThatIsntAccepted then
    print('What was given was also a brick colour!') -- Shouldn't print, as it's not a valid brick colour.
end

Please let me know if you have any questions, or I'm forgetting something. :) Hope this helped! :D

0
hoot hoot TheeDeathCaster 2368 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

You can check if a string is a BrickColor by using the BrickColor.new function.

An example would be:

local text = "Baby blu"
local color = BrickColor.new(text)

if color and string.lower(tostring(color)) == string.lower(text) then
    print'string is color'
end

That will print string is color if the string the user provided is a color or not.

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago
function isbrickcolor(color)
    if tostring(color) == "Medium stone grey" then -- We know for a fact that's a BrickColor
        return true 
    end
    if tostring(BrickColor.new(color)) == "Medium stone grey" then --It can't possibly be Msg.
        return false
    else
        return true -- it didn't get turned to Msg. It must be a brickcolor.
    end
end

Answer this question