How can i save player accessories?

Hello, i have problem with save player accessories and here my code:

local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("AccessoriesSaver")

game.Players.PlayerAdded:Connect(function(Player)
	
	local Data
	
	local Folder = Instance.new("Folder", Player)
	Folder.Name = "Items"
	
	pcall(function()
		
		Data = myDataStore:GetAsync(Player.UserId)
		
		for _, Item in pairs(Data) do
			local Value	= Instance.new("StringValue", Folder)
			Value.Name = Item
		end
		
		Player.CharacterAdded:Connect(function()
			for i, accessory in pairs(Folder:GetChildren()) do
				game.ServerStorage.Accessories[accessory.Name]:Clone().Parent = Player.Character
			end
		end)
	end)
end)

game.ReplicatedStorage.SaveEvent.OnServerEvent:Connect(function(Player)
	pcall(function()
		
		local Wear = {}
		
		for _, Item in pairs(Player.Items) do
			table.insert(Wear, Item.Name)
		end
		
		myDataStore:SetAsync(Player.UserId, Wear)
	end)
end)
2 Likes

Are there any errors in the output?

1 Like

No but the item value not saving and i don’t know why

Have you enabled API in your studio?

1 Like

Yes, but does not saving value

Is it not saving when the player leaves? Or does it not load.

Exactly when the player leaves

Should use PlayerRemoved Event instead of a remote event.

i guess he need use game.Players.PlayerRemoving:Connect(function()

Try changing it to this script real quick so we can see the error.

game.ReplicatedStorage.SaveEvent.OnServerEvent:Connect(function(Player)
	local success, errorMessage = pcall(function()
		
		local Wear = {}
		
		for _, Item in pairs(Player.Items) do
			table.insert(Wear, Item.Name)
		end
		
		myDataStore:SetAsync(Player.UserId, Wear)
	end)
    if success then
      print("it works woohoo!")
    else
      error(errorMessage)
    end
end)

I checked and it still doesn’t work

Print the table when the player leaves.

That’s the result

image

Ah i know now do Player.Items:GetChildren().

What’s the 33th line? Can you give us a screen shot?

Here screenshot:

image

Did changing it to :GetChildren() Work?

Theres a typo, change it to:

for _, Item in pairs(Player.Items:GetChildren()) do

No, the error said table error. (Nevermind)

That’s works thank you so much