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
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)