[Solved] Help with loadstring()

I’ve tried to find the solution to the problem below online, but with no success. Perhaps any of you wizards here know what mistake I’m making?

The following works:

func = loadstring("return 1")
a = func()
print(a) -- will print 1

The following will just print nil. I expect the same behavior as above:

function test()
 return 1

func = loadstring("test()")
a = func()
print(a) -- will print nil. assumed to print 1.

Am I trying to do something impossible? Perhaps I’m missing something when it comes to environment setting?

Any help is highly appreciated!


func = loadstring("return test()")


Thanks ffx! I thought I’d tried everything, but i obviously missed something obvious :stuck_out_tongue: