Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why is the datastore occasionally duplicating or removing items?

I made an item saving/loading script for someone recently, and it works, for the most part. Recently he told me that, though it does save the items, it occasionally duplicates them or removes them. I've tried examining it, but I can't find much of a reason for either problem.

The tools are stored in ReplicatedStorage, and copied into the player's inventory.

local PlayersItems = game:GetService("DataStoreService"):GetDataStore("Items") 

function SaveData(Player)
	local Items = {}
	for i,Item in ipairs(Player.Backpack:GetChildren()) do
		if game.ReplicatedStorage:FindFirstChild(Item.Name) ~= nil then
			if game.ReplicatedStorage[Item.Name]:IsA("Tool") then
				table.insert(Items, Item.Name)
			end
		end
	end
	PlayersItems:SetAsync(Player.UserId, Items)
end

function LoadData(Player)
	if PlayersItems:GetAsync(Player.UserId) ~= nil then
		for i,ToolName in ipairs(PlayersItems:GetAsync(Player.UserId)) do
			local Tool = game.ReplicatedStorage[ToolName]
			Tool:Clone().Parent = Player.Backpack
		end
	end
end

game.Players.PlayerAdded:connect(function(Player)

	game.Players:WaitForChild(Player.Name)
	Player:WaitForChild("Backpack")
	
	LoadData(Player)
		
	Player.CharacterAdded:connect(function()
		LoadData(Player)
	end)	
	
	game.Players.PlayerRemoving:connect(function(Player)
		SaveData(Player)
	end)
	
	Player.Backpack.ChildAdded:connect(function()
		SaveData(Player)
	end)
	
end)

1 comment

0

WaitForChild only yields for a max of 5 seconds. If they have an extremely slow connection it may not work. That's not the problem though, just something to note.

1 answer

It looks like your player or the ReplicatedStorage has the same item twice.

2 comments

0

Can you please elaborate as to why?

0

Cloning usually clones it more then once. (its annoying)

This discussion is preserved as it was. New answers, comments, and votes are closed.