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

How do you check if a variable is a number or a string?

Asked by 5 years ago

How do you check if a variable is a number or a string?

2 answers

Log in to vote
0
Answered by
Zafirua 1348 Badge of Merit Moderation Voter
5 years ago
local variable = --Insert Val

if (type(variable) == "string") then
     --String
elseif (type(variable) == "number") then
    --Number
else
    --Some other
end
0
Just wanted to say, I honestly love the C++ syntax you've got going on. It's always been aesthetically pleasing me lmao. ObscureIllusion 352 — 5y
0
It's habits from Java, I know him from the SH discord server :p User#24403 69 — 5y
Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

Hey SimplyJeremy,

You can do this simply by using type(). Like this:

local num = 1
local str = "Whatever"

if type(num) == 'number' then
    print("It is a number.")
else
    print("It is not a number.")

print(type(num)) -- Prints number
print(type(str)) -- Prints string

Reference:

Roblox Wiki Type

I hope I helped and have a wonderful day/night.

Thanks,

Best regards,

KingLoneCat

0
Error Line 06, finds `:` Error Line 08, expecting end. Zafirua 1348 — 5y
0
Sorry, too used to Python xD KingLoneCat 2642 — 5y

Answer this question