Datastores Table Error? (invalid argument #3 (string expected, got nil)

Hi there, I’ve got this error in the output and I’m not so sure what it means (I haven’t used tables prior to this so I’m pretty unaware)

Output:
Output2 The Error

Output1 Says its “Saved” and shows it

Script:
(Player joined)

local function PlayerJoined(Player) -- leaderstats
	print(Player)
	local leaderstats = Instance.new("Folder",Player)
	leaderstats.Name = "leaderstats"
	local gold = Instance.new("NumberValue",leaderstats)
	gold.Name = "Gold"
	local playerArmors = Instance.new("Folder",Player)
	playerArmors.Name = "Armors"
	local equipped = Instance.new("StringValue",playerArmors)
	equipped.Name = "Equipped"

	local data
	local success, errormessage = pcall(function()
		data = DS:GetAsync(Player.UserId)
	end)

	if success and data ~= nil then
		gold.Value = data[1]

		--equipped.Value = data[2] **(Where i get the error)**

for _, armor in pairs(data) do
			local ownedArmor = Instance.new("StringValue",playerArmors)
			ownedArmor.Name = armor
		end
		warn("Data loaded for ".." "..Player.Name)
	else 
		gold.Value = 10
		local starterArmor = Instance.new("StringValue",playerArmors)
		starterArmor.Name = "Wood"
		starterArmor.Value = "Wood"
		equipped.Value = starterArmor.Value
		warn("New player joined!")
	end 
	Player.CharacterAdded:Connect(onCharacterAdded)
end
(Saving data)
local function PlayerLeft(Player)
	local saved = {}

	table.insert(saved, Player.leaderstats.Gold.Value)
	table.insert(saved, Player.Armors.Equipped.Value)
	for _, armor in pairs(Player.Armors:GetChildren())do
		if armor:IsA("StringValue") and armor.Name ~= "Equipped" then
			table.insert(saved, armor)
		end
	end

	local success, errormessage = pcall(function()
		DS:SetAsync(Player.UserId, game:GetService("HttpService"):JSONEncode(saved))
	end)

	if success then
		warn("Data saved for ".." "..Player.Name)
		print(saved)
	else
		warn("Error while saving data for ".." "..Player.Name.." "..errormessage)
	end
end

Thanks,

You’re encoding the saved table into a JSON string with JSONEncode I don’t know what you’re trying to do but did you forget to decode it back to a table? which is why data[2] is nil

1 Like