Trying to save and load attributes

Hello, developers. I am trying to save attributes (enchantments) for my project I am working on.
My issue that is happening right now is that when I save the attributes to a table, I can’t get print them back out again. However, I can print everything else in that table. I am even having to “decode” the table’s memory using Repr. (Repr – function for printing tables)

However, when decoding using Repr I get this.
image
So there should be no reason for it to error, but it does.

Edit: I don’t know if I’ve mentioned this, but I am debugging this when the player leaves, not when the player joins. The main issue is when the player joins, but it all comes to the saving of the data.

Code
-- IGNORE UNTIL AT PLAYER LEFT FUNCTION --
local Datastore = game:GetService("DataStoreService")
local Data = Datastore:GetDataStore("Data")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local repr = require(3148021300)

function LoadData(Player)
	
end

function PlayerAdded(Player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = Player
	
	local Gold = Instance.new("IntValue")
	Gold.Name = "Gold"
	Gold.Parent = leaderstats
	
	local Weapons = Instance.new("Folder")
	Weapons.Name = "Weapons"
	Weapons.Parent = Player
	
	local success, returned = pcall(function()
		return Data:GetAsync(Player.UserId)
	end)
	
	if success and returned then
		Gold.Value = returned["Gold"]
		
		for _,weapon in ipairs(returned["Weapons"]) do -- Going through the weapon
			local i = Instance.new("Folder")
			i.Name = weapon["Name"]
			i.Parent = Weapons
			
			for name,value in ipairs(weapon["Enchants"][1]) do -- Going through the attributes/enchants of the weapon
				print("RAN")
				print(name, value[name])
				i:SetAttribute(name, value[name])
			end
		end
	else
		print(tostring(success).." : "..tostring(returned))
	end
end

function PlayerLeft(Player) -- ISSUE HERE --
	local Gold = Player:FindFirstChild("leaderstats"):FindFirstChild("Gold").Value
	local Weapons = Player:FindFirstChild("Weapons"):GetChildren()
	
	local WeaponTable = {}
	
	for i,v in ipairs(Weapons) do
		table.insert(WeaponTable, i, {["Name"] = v.Name, ["Enchants"] = v:GetAttributes()}) -- Making a table with the weapon and the weapon's enchants
	end
	
	local PlayerData = {
		["Gold"] = Gold,
		["Weapons"] = WeaponTable
	}
	print(repr(WeaponTable, {pretty=true}))
	for i,v in ipairs(WeaponTable) do
		---print(v["Name"])
		--print(repr(v["Enchants"]))
		for k,l in ipairs(v["Enchants"]) do
			print(k, l) -- Nil for some reason???
		end
	end
	
	pcall(Data:SetAsync(Player.UserId, PlayerData))
end

game:BindToClose(function()
	if RunService:IsStudio() then
		task.wait(3)
		return
	else
		for _,plr in pairs(Players:GetPlayers()) do
			spawn(PlayerLeft, plr)
		end
		
		task.wait(5)
	end
end)

Players.PlayerAdded:Connect(PlayerAdded)
Players.PlayerRemoving:Connect(PlayerLeft)

I’ve been at this for two hours.
Thanks!

  • DeveloperBLK
1 Like

I am not entirely sure. Everything looks right and should work in theory. Maybe the Weapon contents are cleared due to player leaving before the loop has a chance to reference it into the table? But that seems unlikely…

I’ve saved everything in a variable making this impossible. This makes me confused too. :smiling_face_with_tear:

I’ve found out that if I check for the value it will would.

print(WeaponTable[1]["Enchants"]["Fletching"])
print(WeaponTable[1]["Enchants"]["Perun"])

image

Why are you using Ipairs? instead why don’t you just use pairs because you are looping through a dictionary.

1 Like

Because ipairs goes in order. Also, that’s not the problem.

Edit: Never mind xD. ipairs was the problem. Thank you so much!

1 Like