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)()
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:
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.