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

Help with numbers & strings?

Asked by 10 years ago

I have a GUI textbox, and I want the ability to detect if the text that was input was a number, or a letter. How would I do this? I know it has to involve a '~=' and a loop to check it.

1 answer

Log in to vote
0
Answered by 10 years ago

You can check by using the 'type' method, the type method will identify the identifier type of what it is, for example, if you were to use the following code;

local str = "I'm a sting!"
print(type(str)) --Output: string

As you'll see, after firing that code, it has printed string in the Output because str is a string identifier, this same thing will happen for Numbers and Booleans aswell;

local bool = true
print(type(bool)) --Output: boolean

local num = 1
print(type(num))

So, to answer your question, this can also be used in if then end statements to identify certain identifiers, like for example;

local identifier = "string" --A string :o

if type(identifier) == "number" then --If 'identifier' is a 'number' then
--Code
return identifier --Returns the number
elseif type(identifier) == "string" then --Elseif 'identifier' is a 'string' then
--Code
return #identifier --Returns the number of Characters in a string
elseif type(identifier) == "table" then --Surprised? it even works with 'table''s! :D
--Code
return #identifier --Returns the number of string/numbers/ect. in a table
end

Hope this helped!

Ad

Answer this question