How do I execute a string as a piece of code?
For example, if I have:
x = "print('hello')"
is there a function that would let me do something like
run(x)
?
How do I execute a string as a piece of code?
2 answers
There is a feature in Roblox called "LoadString" in which you can execute code. However, this is used by exploiters to exploit in your game, so please be careful when doing this.
In order to enabled "LoadString", go check the box in the properties of ServerScriptService.
loadstring()
6 comments
Thank you. Annitially I had the loadstring enabled in ServerScriptService and I tried it and it wasn't working. But apparently doing 'loadstring(x)()' works but 'loadstring(x)' doesn't. So thank you for that link.
What? Hackers can already hack your game even if loadstring is disabled. They have access to the Roblox executable and access to their computer which the software is running on. It's also in the developer wiki here https://developer.roblox.com/api-reference/lua-docs/Lua-Globals
who downvoted this even tho it's right... ZzZzZZZzzz
did you not read the above comment?
hmmm. Strange, I thought I read it correctly.
Sorry for the mistakes. I searched it up on the developer wiki and no hits came up.
Yes there is, Let's take PlayerAdded as an example.
Your function:
function printing(String)
print(String)
end
PlayerAdded: So when a player is added, it will call the function above ^^ and print the String that is written in the parameter of the called function below vv
Players.PlayerAdded:Connect(function(player)
printing(player.Name.." has joined the server!")
end)
Comment if there is any confusion.
4 comments
I may have misunderstood the original question, but this is just concatenating two strings, i think they want to know if you can actually run a string as code.
Correct ^
Oh. Then Since roblox is running on Lua maybe this can help you: https://stackoverflow.com/questions/44386060/execute-lua-string-as-lua-code
I've tried this. I should mention I've enabled 'LoadStringEnabled' inside serverscriptservice. But I get nothing. No errors and also nothing from the 'print()'. So I don't know whats wrong...
This discussion is preserved as it was. New answers, comments, and votes are closed.

3 comments
Can you give an example where this would be useful? I would just do print("hello")
@MegaManSam1, load script from pastebin. Do you know a way to do this?
I don't, sorry.