DataStores Not Working For Specific Object In Table

I have a DataStore script. I save everything through a table, and use a number(called a MIndex in my script) to save the position of each value in the table. The table data does not save MIndex’es above 10 correctly. It is only these positions that are problematic, and not with any method or consistency.

An image of the explorer setup with data(the MIndex of the values in the “Weapons” folder correspond with the name of the value):

My Code:

local dss = game:GetService("DataStoreService")
local master = dss:GetDataStore("Data")

game.Players.PlayerAdded:Connect(function(plr)
	local folder = script.Stats:Clone()
	folder.Parent = plr
	local data = master:GetAsync(plr.UserId)
	if data ~= nil then
		for _, val in pairs(folder:GetDescendants()) do
			if val:IsA("StringValue") or val:IsA("BoolValue") or val:IsA("IntValue") and val.Name ~= "MIndex" then
				if data[val.MIndex.Value] ~= nil then
					val.Value = data[val.MIndex.Value]
				end
			end
		end
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local data = {}
	for _, val in pairs(plr.Stats:GetDescendants()) do
		if val:IsA("StringValue") or val:IsA("BoolValue") or val:IsA("IntValue") and val.Name ~= "MIndex" then
			table.insert(data, val.MIndex.Value, val.Value)
		end
	end
	master:SetAsync(plr.UserId, data)
end)

Feel free to ask for any details that I didn’t specify in this post, as I may have overlooked some things. Thank you to any and all who can help.

Edit: I did some printing, and realized that the DataStores are saving correctly. Somehow my code is misinterpreting it into the Stats folder.
Edit2: The MInex’es above 10 are probably erroring because of the Coins, Equipped, and Health in positions 11, 12, and 13.

Well your looping through the folder not the weapons folder if that’s what you are trying to save

I’m using :GetDescendants(), not :GetChildren(), so I don’t believe that to be the problem.

Ahh I see, don’t know how I missed that.

Would you have any idea on why this is happening?

I’m a bit confused on what the MIndex is all about

I believe that without it, all the data would get jumbled. It saves in alphabetical order if the position in the table is not specified. If I were to add a new component, everything’s order would be pushed around.

What are the values of 15 & 18?

They are booleans. They represent whether the player owns a certain weapon.

Are you sure they aren’t being saved? Have you printed the data being saved and the data when you load in?

I’m doing that right now. The values seem to always return as false when loading in. The do however, become true after I buy the item.

Ok so, I finished the printing. The values in the DataStore are correct, but they’re not loading into the Stats folder correctly.

After some more extensive printing and switching to ipairs() instead of pairs(), I have found that anything beyond 13 does not print.