Anyway to get the type in a string?

Hello, I would like to get the typoe of thing a variable/constant is. For example

local String = [[
local Constant1 = "Happy"
local Constant2 = 100
local Constant3 = Vector3.new(0, -1, 0)
local Constant4 = CFrame.Angles(0, math.rad(45), 0)
]]

Constant1 should return string, Constant2 should return number, Constant3 should return Vector3 and Constant4 should return CFrame. I want to be able to do this without a bunch of elseif and string.matches. I looked into loadstring but it only returns nill or function not number, string, etc…

does anyone have a good way of doing this or is the only way a bunch of elseif statements?

You should try

print((type(thing goes here))

EDIT 1: This should print out the type of anything you put between the parentheses of “type”

@Mememoxzine That doesn’t work for roblox types, it’ll return userdata for Vector3

@xDeltaXen Use typeof, it works for Roblox types as well, place the thing to check the type of in the brackets beside typeof

print(typeof(Constant1)) --string

Oh, thanks for telling me that!

1 Like

Are you sure using loadstring doesn’t work?

local String = [[
local Constant1 = "Happy"
local Constant2 = 100
local Constant3 = Vector3.new(0, -1, 0)
local Constant4 = CFrame.Angles(0, math.rad(45), 0)
print(typeof(Constant1), Constant1, typeof(Constant2), Constant2, typeof(Constant3), Constant3, typeof(Constant4), Constant4)]] 

loadstring(String)() -- loadstring returns a function, ensure you're calling it by adding the two parentheses at the end or call it as a normal function elsewhere in the script by defining it as a variable

Outputs:

string Happy number 100 Vector3 0, -1, 0 CFrame 0, 0, 0, 0.707106769, 0, 0.707106769, 0, 1, 0, -0.707106769, 0, 0.707106769

Ensure the LoadstringEnabled property of ServerScriptService is true (unless you’re using the command bar, loadstring should work there regardless).

Thanks, I was using string.match and then using loadstring on the value of the constants.

Issue how do I get the value that loadstring(String)() returns? As I am using a plugin I cannot do “print(typeof(Constant1), Constant1, typeof(Constant2), Constant2, typeof(Constant3), Constant3, typeof(Constant4), Constant4)” i can only work with .source as I am making a format script

I’m a little confused as to what you mean by “the value it returns”. Do you mean the function loadstring returns or are you trying to return something from inside of the string?

If the former, just don’t call the function.

local String = [[
local Constant1 = "Happy"
local Constant2 = 100
local Constant3 = Vector3.new(0, -1, 0)
local Constant4 = CFrame.Angles(0, math.rad(45), 0)
print(typeof(Constant1), Constant1, typeof(Constant2), Constant2, typeof(Constant3), Constant3, typeof(Constant4), Constant4)]] 

local myFunction = loadstring(String)

-- Then later in the script, call it as normal

myFunction()

If the latter,

local String = [[
local Constant1 = "Happy"
local Constant2 = 100
local Constant3 = Vector3.new(0, -1, 0)
local Constant4 = CFrame.Angles(0, math.rad(45), 0)
return {Constant1; Constant2; Constant3; Constant4}
]]

local returnedValues = loadstring(String)()

print(returnedValues) -- Prints a table containing the info from the return above

Or if you’re trying to pass arguments TO the function, it’s a little more difficult. When you call a function without passing a predefined set amount of values to be passed, we use ... which contains all of the arguments.

If we use loadstring on

'print(\'a\')'

, it’s the equivalent of doing

local function functionName(...)
    print('a')
end

To get “access” to these variables, we can wrap them in a table.

local myVariables = {...}

So if we want to use loadstring on them, we can do something like this:

local myFunction = [[
local allVariables = {...} -- ... is predefined when using loadstring
print('Passed Variables')
for i,v in ipairs(allVariables) do
    print(v, 'is type '..typeof(v))
end
]]

local function exampleFunction()
end

loadstring(myFunction)('a', 3, {cool = 'table'}, exampleFunction)

Outputs:

  13:39:00.767  Passed Variables  -  Edit
  13:39:00.767  a is type string  -  Edit
  13:39:00.767  3 is type number  -  Edit
  13:39:00.768   â–Ľ  {
                    ["cool"] = "table"
                 } is type table  -  Edit
  13:39:00.768  function: 0x3e7e3ce91d12bf25 is type function  -  Edit

For example this

local Constant1 = "Happy"
local Constant2 = 100
local Constant3 = Vector3.new(0, -1, 0)
local Constant4 = CFrame.Angles(0, math.rad(45), 0)

is code in a script, I want to format it so istead of Constant1, Constant2, etc it is StringConstant, NumberConstant, etc. And the only valid way way I have done this on a large scale is gmatch but that takes ages because I need to do it for CFrame, Vector3, Functions. I have been looking for more easy and better ways. I can’t add to the .Source return typeof(Whatever). So it’s either GMatch or some other method that I don’t know of, aka why I am asking here