Issues with loading and saving inventory data store

Recently I have had some problems with loading and saving player’s inventories. While I had people testing the game this never occurred, but when there are around 5+ people in the server, it has a chance of not saving and loading a player’s items.
Would anyone know how to fix this issue?

game.Players.PlayerAdded:Connect(function(player)
	
	local inventory = Instance.new("Folder",player)
	inventory.Name = "Inventory"
	local boomboxes = Instance.new("Folder",inventory)
	boomboxes.Name = "Boomboxes"
	local equipped = Instance.new("Folder",boomboxes)
	equipped.Name = "Equipped"
	
	
	local character = player.Character
	if not character or not character.Parent then
		character = player.CharacterAdded:wait()
	end
	
	local playerBoomboxes = Instance.new("Folder",character)
	playerBoomboxes.Name = "Boomboxes"
	local playerEquipped = Instance.new("Folder",playerBoomboxes)
	playerEquipped.Name = "Equipped"
	
	
	
		-- Loading inventory
	local success, errormessage = pcall(function()
		local boombox = inventorydataStore:GetAsync("User-"..player.UserId)
		if boombox then
			for i, v in pairs(boombox)do
				local boomboxFound = boomboxList:FindFirstChild(v)
				if boomboxFound then
					boomboxFound:Clone().Parent = boomboxes
				end
			end
		end
	end)
	
	
	
	
	-- Loading equipped boombox
	local success, errormessage = pcall(function()
		local boombox = equippedDataStore:GetAsync("User-"..player.UserId)
		if boombox then
			for i, v in pairs(boombox)do
				local boomboxFound = boomboxList:FindFirstChild(v)
				if boomboxFound then
					boomboxFound:Clone().Parent = equipped
					local boomboxToEquip = boomboxFound:Clone()
					
					local Weld = Instance.new("WeldConstraint")
					
					if player.Character then
						wait(0.5)
						local torso = player.Character.UpperTorso
						boomboxToEquip.CFrame = torso.CFrame * boomboxToEquip.CFrame
						Weld.Parent = player.Character
						boomboxToEquip.Parent = player.Character.Boomboxes.Equipped
						Weld.Part0 = player.Character.UpperTorso
						Weld.Part1 = boomboxToEquip
					else
						player.CharacterAdded:Connect(function()
							wait(0.5)
							local torso = player.Character.UpperTorso
							boomboxToEquip.CFrame = torso.CFrame * boomboxToEquip.CFrame
							Weld.Parent = player.Character
							boomboxToEquip.Parent = player.Character.Boomboxes.Equipped
							Weld.Part0 = player.Character.UpperTorso
							Weld.Part1 = boomboxToEquip
						end)
					end
				end
			end
		end
		if not boombox then
			
			local Weld = Instance.new("WeldConstraint")
			
			local boomboxToEquip = boomboxList.goldenSuperFlyBoombox:Clone()

			local equippedBoombox = boomboxToEquip:Clone()
			local inventoryBoombox = boomboxToEquip:Clone()
			if player.Character then
				wait(0.5)
				local torso = player.Character.UpperTorso
				boomboxToEquip.CFrame = torso.CFrame * boomboxToEquip.CFrame
				Weld.Parent = player.Character
				boomboxToEquip.Parent = player.Character.Boomboxes.Equipped
				Weld.Part0 = player.Character.UpperTorso
				Weld.Part1 = boomboxToEquip
				equippedBoombox.Parent = equipped
				inventoryBoombox.Parent = boomboxes
			else
				player.CharacterAdded:Connect(function()
					wait(0.5)
					local torso = player.Character.UpperTorso
					boomboxToEquip.CFrame = torso.CFrame * boomboxToEquip.CFrame
					Weld.Parent = player.Character
					boomboxToEquip.Parent = player.Character.Boomboxes.Equipped
					Weld.Part0 = player.Character.UpperTorso
					Weld.Part1 = boomboxToEquip
					equippedBoombox.Parent = equipped
					inventoryBoombox.Parent = boomboxes
				end)
			end
		end
	end)

	
end)






-- Saving data
game.Players.PlayerRemoving:Connect(function(player)
	local success, errormessage = pcall(function()
		local boomboxInventorySave = {}
		for i, boombox in pairs(player.Inventory.Boomboxes:GetChildren()) do
			if boombox then
				table.insert(boomboxInventorySave,boombox.Name)
			end
		end
		inventorydataStore:SetAsync("User-"..player.UserId,boomboxInventorySave)
	end)
	
	local success, errormessage = pcall(function()
		local boomboxEqippedSave = {}
		for i, boombox in pairs(player.Inventory.Boomboxes.Equipped:GetChildren()) do
			if boombox then
				table.insert(boomboxEqippedSave,boombox.Name)
			end
		end
		equippedDataStore:SetAsync("User-"..player.UserId,boomboxEqippedSave)
	end)
end)

As you’re saving the player’s data with PlayerRemoving, which fires after player disconnects, you’d also want to utilise game:BindToClose to save data externally. This event fires when the server closes, or the last player disconnects, and prevents the server from shutting down the game before PlayerRemoving is fired. Therefore, save data in BindToClose as your secondary option.

2 Likes