Why are my items duplicating to the other player?

I’ve tried fixing this in many attempts and they never work. When I join a server with two or more players the second player would gain all the string values that first player had. I don’t know why it’s doing this so any help would be appreciated.


--Grabbing the Players Data and Putting it into strings.
if Datastore:GetAsync(player.UserId) then 
     for i,v in pairs(Datastore:GetAsync(player.UserId)) do
 		print(player.Name.." Name: " .. v[1] .. " || Class: " .. v[2])
 		local Item = Instance.new("StringValue", inv)
     	Item.Name = v[1]
     	Item.Value = v[2]
     end
 else
 end
--Saving The Players Inventory Folder Into a Table to then Get.
local function OnPlayerRemoving(Player)
 for i,v in pairs(Player.InventoryFolder.Inventory:GetChildren()) do
 	print(Player,v)
 	table.insert(itemlist, {v.Name, v.Value})
 end
 	Datastore:SetAsync(Player.UserId, itemlist)
 	print("Saved Inventory")
end

game.Players.PlayerRemoving:Connect(OnPlayerRemoving)```

Is there a reason that when you grab the data you use the player’s ID as the key, but when you save to the data store you use the player’s ID plus the extra “Player_” string?

Your saving the data to a different key than your getting it from.

Sorry I forgot to change that XD. My problem isn’t fix even with that change. Idk why and I’m having trouble figuring it out.

The problem is because of you itemlist variable, you have it as a global variable and is never setback back to an empty table in your code snippet, so other players’items are added to it. Move the itemlist variable inside the OnPlayerRemoving block.

1 Like