Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

What are the uses of 'LoadString'?

I was checking out the 'Scripting Book' in the ROBLOX wiki, and I saw LoadString, and I heard of it before; I check it out and pretty much, I say to myself "This was pretty easy! But wait, why is this useful?" I also noticed that for some scripts, this is relevant to DataStores Anyone can tell me why it's useful?

code = [[ p = Instance.new("Part", game.Workspace) p.Anchored = true ]]
loadstring(code)()

1 answer

✓ Accepted answer

Rarely is loadstring a good option

You use loadstring when you need something to be done on demand, but you don't have any idea what that code would be. For example -- Script Builders -- since you cannot make some function which will account for everything people want to do, you have no choice but to just let them execute arbitrary Lua.

Other things that might use it:

  • What players say (like Script Builders)
  • Files / StringValue resources
  • Saved data (DataStores, HttpGet)

Note that loadstring is brittle, and not safe (a simple workspace:BreakJoints() will ruin everything else). It isn't a good solution.

For instance, to make parts, like in that example, it would be better to serialize information about it rather than actually use code:

Part,game.Workspace,Anchored=true

and then parse that instead.

This discussion is preserved as it was. New answers, comments, and votes are closed.