I’m trying to create a plugin where you can create files and store them in a file explorer, but I can’t find an acceptable time to save data since if I do it when a file is changed that could cause a datastore throttle.
I would like to see if there is an event that fires when an editor is exiting Roblox Studio, allowing data to be simply saved when a user leaves and retrieved when they re enter.
Could not find any events while searching so I’m relying on the devforum. Any help is appreciated!
AFAIK, there are no ways to detect this and all the events for StudioService are locked to CoreScripts only. To my knowledge, there is no limit to plugin:SetSetting() (datastores don’t save across games in plugins) but it is probably best not to run it too often. If it is possible, you should save whenever a user closes a file. In addition to this, you can connect an event to plugin.Unloading which runs when a plugin is closed/uninstalled/updated so that all data is saved at the end of sessions. I don’t know if these are likely to fail or not, but if you find that they do then I would suggest adding a save every 30-60 seconds.
Edit: If you need to save each time text changes, I use this in one of my plugins. It’s a little extreme on the saving and I would recommend dialling it down (I use it for a small widget that won’t usually be open longer than a few seconds and would have a large impact on UX if it used an out of date save).
local RunService: RunService = game:GetService("RunService")
local TextLastSave: number = 0
local TextLastChange: number = 0
local LastText: string = ""
local TextBox: TextBox -- TextBox reference
TextBox:GetPropertyChangedSignal("Text"):Connect(function()
TextLastSave = os.time()
TextLastChange = TextLastSave -- Keeps them in sync, and looks nice (might be quicker/slower idrc)
LastText = TextBox.Text
end)
RunService.Heartbeat:Connect(function()
local Time: number = os.time()
-- 1 second since last change, 3 seconds since last save or difference in length >= 5
if Time - TextLastChange >= 1 or Time - TextLastSave >= 3 or math.abs(#LastText - #TextBox.Text) >= 5 then
plugin:SetSetting("YOUR_KEY_HERE", TextBox.Text)
end
end)
Thank you for the help but I have one problem with what you said about StudioService, I was able to run something from it and it worked fine so aren’t plugins core scripts?
You can use some of the StudioService things (I use .ActiveScript in a few of my plugins), but not all of them. For example, connecting to .OnSaveOrPublishPlaceToRoblox will cause an error since it is only for CoreScripts.
Would have been fun if plugins were granted a little more access to the Service but oh well! Thank you for a quick response to my problem, I’ll try it out and message back to you