My Tools Duplicate Whenever I Start My Game

Hello There!
so i have a problem which when i join a game it duplicates the tool i have.
there is only TWO ways that this is happening but since im new to scripting i dont know which
and how to fix it so here are the ways:
1.is there is a bug in the starter pack
and lastly 2.is there is a problem with my datastore script
here. its a normal script and its on “ServerScriptService”

local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetDataStore("Inventory2")

game.Players.PlayerAdded:Connect(function(player)
	pcall(function()
		local tool = dataStore:GetAsync("User-"..player.UserId)
		if tool then
			for i,v in pairs(tool) do
				local toolfound = game.ReplicatedStorage.Items:FindFirstChild(v)
				if toolfound then
					toolfound:Clone().Parent = player.Backpack
					toolfound:Clone().Parent = player.StarterGear
				end
			end
		end
	end)
end)

game.Players.PlayerRemoving:Connect(function(player)
	pcall(function()
		local toolSave = {}
		for i, tool in pairs(player.Backpack:GetChildren()) do
			if tool then
				table.insert(toolSave,tool.Name)
			end
		end
		dataStore:SetAsync("User-"..player.UserId,toolSave)
	end)
end)

ThankYou!!

1 Like

I think the duplication would come from toolfound:Clone().Parent = player.Backpack or toolfound:Clone().Parent= player.StarterGear

Have you tried removing one of these? Since you basically are giving the player a gear twice.

No Yet actully but i will try
Thank You!

Basically StarterGear is like StarterPack except for an individual player. So whenever a tool is added to the StaterGear folder it will clone them to the Backpack. If you want your tools to save when the player dies use StaterGear but if you want them to go away when they die use Backpack.

i tried to edit the script is this right tho??
well i just added a delete

local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetDataStore("Inventory2")

game.Players.PlayerAdded:Connect(function(player)
	pcall(function()
		local tool = dataStore:GetAsync("User-"..player.UserId)
		if tool then
			for i,v in pairs(tool) do
				local toolfound = game.ReplicatedStorage.Items:FindFirstChild(v)
				if toolfound then
					toolfound:Destroy()
					toolfound:Clone().Parent = player.Backpack
					toolfound:Clone().Parent = player.StarterGear
				end
			end
		end
	end)
end)

game.Players.PlayerRemoving:Connect(function(player)
	pcall(function()
		local toolSave = {}
		for i, tool in pairs(player.Backpack:GetChildren()) do
			if tool then
				table.insert(toolSave,tool.Name)
			end
		end
		dataStore:SetAsync("User-"..player.UserId,toolSave)
	end)
end)
```lua

I assume you want the player to keep the tools when they die?

Have you tried this?

if toolfound then
	toolfound:Clone().Parent = player.StarterGear
end

But Dont i need to put it in the backpack too??
cuz im thinking if i remove the tool first then the tool wont save.

StarterGear will put it in the Backpack.

If you want the tools to be kept with the player when they die use StarterGear or if you want the tools to go away use the Backpack.

1 Like

right but how do i make it so it saves when they quit the game and it does not duplicate??

i removed the backpack clone thing