I'm making a plugin and I'm trying to save a table as a setting. I'm pretty sure you can call :SetSetting() passing a table as a parameter, but it doesn't seem to work. Here's the code:
script.SaveSource.OnInvoke = function(source) print("Saving source:",source.Name, "which is a", type(source)) savedSources[source.Name] = source print("savedSources is a", type(savedSources), ", list of elements:") for i, value in pairs(savedSources) do print(">", i, value) end plugin:SetSetting(SETTING_KEY, savedSources) local loaded = plugin:GetSetting(SETTING_KEY) print("Loaded", loaded, " which is a ", type(loaded)) end
So, savedSources
is a table which contains source
objects, so I'm attempting to save the savedSources table as a setting for my plugin. But it doesn't seem to be working. Here's what it prints out when I try to save:
Saving source: asdfasdf which is a table savedSources is a table, list of elements: > asdfasdf table: 3590481C Loaded nil which is a nil
As you can see, it has loaded nil, even though I have just saved a value.
You cannot save tables as values in plugin settings. You can, however, save a string table that is JSON encoded.
local toSave = HttpService:JSONEncode(savedSources) -- Define HttpService. plugin:SetSetting(SETTING_KEY, toSave) local Xloaded = plugin:GetSetting(SETTING_KEY) local loaded = HttpService:JSONDecode(Xloaded)