How do I run a code from an input?

So, what I am doing is running a code from an input, it is not working, also doesn’t give me errors. I have the server script.

game.ReplicatedStorage.Code.OnServerEvent:Connect(function(player, code)
    print(loadstring(code)) -- The code is just print("Hello"), but it just prints function: 0x25e043b4d07fb9f8
end)

Try doing just the loadstring(code) part instead of print, I believe its because you’re trying to print a function, which basically returns its memory index.

Also, I removed the print(), but still doesn’t work.

loadstring() returns a function, so you would have to do loadstring(code)(). Be sure to sandbox the loaded code(or just don’t use loadstring) or people can break your game really badly.

How do I sandbox it? Do you know?

1 Like

I’m not that experienced with sandboxing stuff, if I were you I would not do this. Why do you need loadstring?

1 Like

http://lua-users.org/wiki/SandBoxes
tl;dr you restrict the environment the code is running in.

1 Like

Not using it in any game. I just want to test it just curious

What do I basically do with this link ;-;

1 Like

Replace your print line with loadstring(code)() if you wanna run it. For sandboxing you can overwrite globals such as getfenv, game etc with nil values (blacklist) or use setfenv (whitelist) to restrict which functions can be used.

Oh, I don’t need to sandbox it. I am just testing the loadstring function. Thank you!