How to convert a mixed table to a non mixed table

So I am trying to make save a mixed table to datastore since datastore doesnt support em how do I convert them and also why is

game.ReplicatedStorage.Update.OnServerEvent:Connect(function(plr,thang)
	local stuff = {
		magic = {
			null = true,
			fire = false,
			air = false,
			earth = false,
			lightning = false,
			ice = false
		},
		weapons = {
			classic = true,
			sword = false,
			spear = false,
			axe = false
		},
		subs = {
			null = true,
			kaboom = false,	
			MayTheForceBeWithYou = false,
		},
	}
	
	local mags  =	plr.SkillSet.Magic:GetChildren()
	for name,bool in pairs(mags) do
		stuff.magic[name] = bool.Value
	end
	for name,bool in pairs(plr.SkillSet.Subs:GetChildren()) do
		stuff.subs[name] = bool.Value
	end
	for name,bool in pairs(plr.SkillSet.Weapons:GetChildren()) do
		stuff.weapons[name] = bool.Value
	end

	DataStore2("SkillSet",plr):Set(stuff)
end)

making stuff a mixed table?

1 Like

name variables in those loops are numbers since :GetChildren() returns instances in an array and stuff table is created as a dictionary.

2 Likes

oh thx Ima try changing name to bool.Parent but still is there a way to convert mixed tables to normal ones there is a high chance of me needing it

Well there is no built in function does that but you can avoid creating one in many ways by:
a. Turning value of names to string using tostring():

local mags  =	plr.SkillSet.Magic:GetChildren()
for name,bool in pairs(mags) do
	stuff.magic[tostring(name)] = bool.Value --Bad idea, don't do this:
end

or
b. Saving values of BoolValues in another table that is an array inside the dictionary(Recommended option):

local stuff = {
	magic = {
		null = true,
		fire = false,
		air = false,
		earth = false,
		lightning = false,
		ice = false,
		extradata = {}
	}
}	
local mags  =	plr.SkillSet.Magic:GetChildren()
for name,bool in pairs(mags) do
	stuff.magic.extradata[name] = bool.Value
end

or
c. Don’t try to save a number at all like you said name the keys after boolvalue’s name(Do not name keys after boolvalue’s parent because that will override them):

local mags  =	plr.SkillSet.Magic:GetChildren()
for name,bool in pairs(mags) do
	stuff.magic[bool.Name] = bool.Value --Don't use this option if there are boolvalue's with same names inside the folder.
end

or if your plan was to override default values in dictionaries with boolvalue’s then option c will work for you.

1 Like