Datastore not saving multiples of the same item?

When I save a players data with multiples of the same items, it doesn’t save all the multiples and only saves the 1

-- Players data in game
Weapons = {
    ['Starter'] = 'Common',
    ['Wood'] = 'Common',
    ['Wood'] = 'Common',
    ['Wood'] = 'Common',
    ['Wood'] = 'Common',
}

When you leave, the data store definately saves, as all other data saves, but their weapons only show

Weapons = {
    ['Starter'] = 'Common',
    ['Wood'] = 'Common',
}

Why isn’t it saving all multiples?

return function(player)
	print('Saving')
	if not DataLoaded.Value then return end
	print('Ready')
	local PlayerData = Converter.ConvertToTable(player.PlayerData)
	local TryCount = 0
		
	local Success, Error
	
	-- Save data
	repeat
		Success, Error = pcall(function()
			DataStore:SetAsync(player.UserId, PlayerData)
		end)
		
		TryCount = TryCount + 1
	until TryCount >= Tries or Success
	print('Success')
	-- Failed to save
	if not Success then
		print('Failed')
		warn('Data failed to save | Error Code:' .. tostring(Error))
					
		return
	end
end

This is because you’re setting the same key (“Wood”) to a value multiple times. The table simply reads that as you editing the key “Wood” to the value “Common.” Any table can only have a certain key contain one value

1 Like

What @Chatowillwin said. I would recommend saving a table which contains the “Common” string as well as how many the user has, as it’s overwriting the key each time.

1 Like