Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

What are the uses of 'LoadString'?

Asked by 9 years ago

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

Log in to vote
2
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

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.

Ad

Answer this question