DataStore overwritten item with same name?

Hey, guys, I just started using the DataStoreService, the load / save works fine, but I’m having issues with items(Tools) with the same name. For some reason, items with the same name get overwritten. How to avoid it?

I also have a IntValue Named “CodeId” inside each item that are 6 random digit numbers(the numbers generate when player buy an item on the shop). I use “CodeId” to find it by the Id and not by Name. I use this method to auto equip items when the player joins the game because ObjectValues cannot be saved in DataStore.

function playerLoad(player)
	if not waitForDataReady(player) then
	module:Notitication(player, 'Save Error(Time Out.)')
	return 
	end

	local SaveData = MyDataStore:GetAsync(player.userId)
	if SaveData then

		for Item,Code in pairs(SaveData) do
		local gameData = GetItems()
			-- gameData return all Tools in game on ReplicatedStorage
			for _,data in pairs(gameData) do
                    -- unfortunately, is overwritting items with the same Name
    				if Item == data.Name then
    				data:Clone().Parent = player:WaitForChild('Backpack')
    				data.CodeId.Value = Code
    				if DEBUG then print(player.Name..'('..player.userId..')','Items Restored: ',Item) end
    				end
    			end										
    		end
    	else
    	if DEBUG then warn(player.Name..' has no Data to Load.') end
    	end
    ActiveBackpack:FireClient(player)
    end
    
    function playerSave(player)
    	if not waitForDataReady(player) then
    	module:Notitication(player, 'Load Error(Time Out.)')
    	return 
    	end	
    	
    	pcall (function()
    	local saveItems = {}
    		for i,items in pairs(player.Backpack:GetChildren()) do
    			if items then
    			saveItems = {[items.Name] = items.CodeId.Value}
    			print('Saving Items...',items)
    			end	
    		end	
    	MyDataStore:SetAsync(player.userId,saveItems)
    	end)
    end

When I leave / When I Join
2019-04-11
I can’t change the Name of the tool because i’ll not able to find it on the table “GameData”
I couldn’t find another way to solve this yet. Does anyone have any soluction for this?
Thank you! (Sorry for English, It’s a work in progress)

2 Likes

The main problem is not Loading stuff, is saving.

for i,items in pairs(player.Backpack:GetChildren()) do
        			if items then
                           -- I'm not inserting in the table, I actually overwritting it.
        			saveItems = {[items.Name] = items.CodeId.Value}
        			print('Saving Items...',items)
        			end	
        		end	

all what i need to do is to use table.insert

table.insert(saveItems,i,{['Name'] = items.Name, ['Id'] = items.CodeId.Value})

1 Like

Since you’re the one who fixed it, you can either press the check under your current reply or post what you did to solve it in a new reply and then press the checkmark underneath that reply to mark it as a solution. Don’t delete it since other people could have a similar issue in the future and this could be helpful for them.

1 Like