lua

Is there a way to make a custom loadstring() function in lua?


Here is an example

local CustomLoad = function(l) loadstring(l) end

CustomLoad("print('hi')")

Please let me know, beacuse I got into this like a week ago, and I've been trying to make it, yet I coudln't. Can some of you tell me if it's even possible?


Solution

  • If you aren't trying to run "print('hi')" in that example, then I do believe you're missing a return statement.

    local CustomLoad = function(l) return loadstring(l) end
    

    But if you are, then:

    local CustomLoad = function(l) return loadstring(l) end
    CustomLoad("print('hi')")()
    

    or

    local CustomLoad = function(l) local f = loadstring(l); f() end
    CustomLoad("print('hi')")
    

    Because loadstring creates a function, which, when run, executes the code in the string. Hope that helped!