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.
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.
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