Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Question with loadstring(String)

I was trying to do tests on creating equations in string forms and using them to calculate numbers. It is supposed to print "10", but it is printing "nil".

local E = "X+Y" local X = 9 local Y = 1 local A = 0

A = loadstring(E) print(A)

Do I have something not working/missing to solve the equation?

1 answer

✓ Accepted answer

Your code needs to look like this:

local E = "X+Y" 
X = 9 
Y = 1

A = loadstring("return " .. E)() print(A)

First off, you need to call the function returned by loadstring with () at the end of loadstring(String). Second, you need to return E as I have shown.

Also, the variables will not carry over to the loadstring function unless you make them global (not _G). My example prints 10.

1 comment

0

That works. Thanks. Ill be able to use it on a larger scale soon. :)

This discussion is preserved as it was. New answers, comments, and votes are closed.