Plugin:GetSetting() and/or plugin:SetSetting(): keys may not contain certain chars

One of the most annoying bugs to track down. Not the most annoying one though.

It appears that the plugin:GetSetting() and plugin:SetSetting() will not work with any keys that contain certain chars. See the following code.

value = math.random(1000, 9999)
key = "somekey"
print("Saving " .. value .. " as " .. key)
plugin:SetSetting(key, value)
print("Loading from " .. key .. ": " .. (plugin:GetSetting(key) or "nil"))

Using this code in a plugin will successfully save and reload the number to “somekey”. However, if you change the key variable to “some.key” or anything with an invalid character in it, plugin:GetSetting() no longer loads the newly saved number and returns nil instead.

There are several characters that cause this to happen. Using this code I have determined which cause the bug:

[code]local plugin = plugin
local buggedChars = {}

for i = 1, 255 do
– Create a random value and the key to store it in
local value = math.random(1000, 9999)
local key = “test” … string.char(i) … “key”

-- Save the value to the key
plugin:SetSetting(key, value)

-- Check if the key saved correctly
if plugin:GetSetting(key) ~= value then
	-- If not, then it's a bugged char
	table.insert(buggedChars, i .. " (" .. string.char(i) .. ")")
end

end

– Print bugged characters.
print(“Bugged chars:”)
print(table.concat(buggedChars, ", "))

– Check if plugin settings loaded.
if plugin:GetSetting(“didLoad”) then
print(“Plugin settings loaded OK.”)
else
print(“Plugin settings did not load.”)
end
plugin:SetSetting(“didLoad”, true)[/code]

The results of the above code are always this:

16:33:41.243 - Unable to parse settings file for plugin
16:33:41.245 - Unable to parse settings file for plugin
16:33:41.311 - Unable to parse settings file for plugin
16:33:41.311 - Unable to parse settings file for plugin
Bugged chars:
13 (
), 34 ("), 46 (.), 92 (\)
Plugin settings did not load.

Running the code more than once will still cause the “Plugin settings did not load” message. Basically, this will invalidate your plugin settings. This was tested with only the .rbxm form of plugins, not the ones installed through the website. We can see that the invalid characters for plugin keys are newlines (13), double quotes (" 34), periods (. 46), and backslashes (\ 92).

5 Likes

You should do it from 0, 255 because it’s possible it could also not save null characters properly.

You could also just say that keys are not sanitized.