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