Is there any type of value that can hold dictionaries or tables?

Something like an IntValue contains an integer value, but are there any values that can store dictionaries or tables? If not, are there any way to store a table in a way that different LocalScripts can access and do changes to the table? Somebody suggested that I should JSONEncode the table then store it inside a StringValue then just JSONDecode it at the time of use, but I will not be storing strings nor numbers in the table. Any help would be greatly appreciated!

There isn’t a specific object that can hold them. If you don’t wnat to use JSONEncode and JSONDecode, then you basically have 2 options:


You can attempt to store it as a global variable, using _G. However, this is generally not adviced due to the potential overwriting of the values stored their by Core scripts, and is also (apparently) not very performant.


You could store the dictionary / table as a collection of ObjectValues, instead of as a single one. Below is some code that I spent far too long on creating that achieves this. The decoding of it is left as an exercise to the viewer (I can’t be bothered)

Code
local Output = game.Workspace
local Array = {"Hello", 123, game.Workspace, ["Bob"] = {5, 2, 3, ["aaaaa"] = "b", ["aagsio"] = 5}}

local function Recursive(data, parent, key) 
	local Holder
	local create = Instance.new -- Just because it gets too messy. 
										 -- You can replace all occurances of create with Instance.new if you prefer
	
	if typeof(data) == "table" then
		Holder = Instance.new("Folder", parent)
		Holder.Name = key
		for key, val in pairs(data) do
			Recursive(val, Holder, key)
		end      
		return
	elseif typeof(data) == "Vector3" then
		Holder = create("Vector3Value")
		
	elseif typeof(data) == "Color3" then
		Holder = create("Color3Value")
		
	elseif typeof(data) == "string" then
		Holder = create("StringValue")
		
	elseif typeof(data) == "number" then
		Holder = create("IntValue")
		
	elseif typeof(data) == "Instance" then
		Holder = create("ObjectValue")
		
	else
		warn("Missing data type for", data)
		return 
	end
	Holder.Name = key
	Holder.Parent = parent
	Holder.Value = data
end

Recursive(Array, Output, 'Dictionary')

image

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.