[PLUGINS] Save & Load Scripts

I’m looking to create a plugin that allows a user to save and load scripts. I’ve got a good history of Roblox lua behind me, but have never worked with plugins before.

The plugins documentation is pretty vague, and I couldn’t find anything else online.

Just wondering what’s the most efficient way to save a script for the user, and be able to load it in another studio window.

Not really sure if cloning the selected instance, then parenting to the plugin would work? I’m not sure.

All help is greatly appreciated :grin:

You’d want to use the plugin.SetSetting and plugin.GetSetting APIs to save the script data. It will always retrieve the latest version of the plugin setting value.

You can retrieve the script data using either script.Source or ScriptEditorService:GetEditorSource, note that the latter is whats in your script window, so might be different to Source if you’re in a live scripting environment. I generally recommend the latter though.

To update a script, you should use ScriptEditorService.UpdateSourceAsync, which uses a similar mechanism to DataStore.UpdateAsync.

2 Likes

Super helpful! Just wondering, what’s the most efficient method of actually saving the script?

If your willing, could you provide a small snippet of code, might help me understand. Thank you

EDIT: Found the solution by just reading what you said! Tyvm

If you want to go with EditSourceAsync and GetEditorSource (i’d prefer this approach because it keeps it in sync with Drafts or your local copy of Live Scripting).

local function saveScriptSource(key: string, scriptToRead: LuaSourceContainer)
  local stream = ScriptEditorService:GetEditorSource(scriptToRead)
  plugin:SetSetting(key, stream)
end

local function getScriptSource(key: string, scriptToWriteTo: LuaSourceContainer)
  local stream = plugin:GetSetting(key)
  if type(stream) ~= "string" then return end -- no valid content

  ScriptEditorService:UpdateSourceAsync(scriptToWriteTo, function()
    return stream
  end)
end
1 Like

Awesome man! Thanks for all your help.