Call built-in lua functions via string (without loadstring)

Hello.
I was wondering if there is a way to call built in lua functions (like require, print, etc) by using a string AND without loadstrings enabled.
An example of this would be

table["FunctionName"](Parameters)

What I’m trying to do is to call print using a string. I think it might look something like this:

["print"]("Hello, World!") -- How to make this work!?

Anyway to do this without loadstring? I understand it may be a security risk, I just enjoy new ways of writing code.

using vLua module which is the same as loadstring but allows you to use it on client and disable ServerScriptService.LoadStringEnabled to prevent exploit injections that use loadstring

Can you show me a code example of implementing this?

You can do this with getfenv()
getfenv returns the environment of a given function or script. It returns a table for all declared function in the script so far.

local a = 'thing'
b = 'another thing'

local env = getfenv()
print(env) -- {['b'] = 'another thing'}
print(env['a'])  -- 'thing
print(env['print']) -- function 0xblahblah

the reason why a or print doesn’t appear in the actual table is because it doesn’t store builtins or local variables in this table so unfortunately you can’t loop through all the variables in a script. However when indexed it returns the value of the variable through metatables which is why env[‘a’] returns something even though there is nothing in the table.

You can read more here and I only understand about 90% of what getfenv does cause it has a parameter. But if you wanna get the builtin functions then its useful cause almost all env’s (unless manipulated return the same builtin functions.

I think he meant he doesn’t wanna use loadstring in general

local Loadstring = require(vLua)

Loadstring("print('Test')")()

this module does not use built in loadstring function to parse string into code
you can inspect this module to study how exactly it works if you want to

Nice, but how do I call the print function using this method?

getfenv()['print']('hello!') -- 'hello!'

Thanks! @V_ladzec I will remember your suggestion for later.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.