Add Respawn ItemsSave function to InventorySystem

Hi everyone I’m currently working on an InventorySystem u can say and rn I have set in my ScreenInventoryGui ResetOnDeath to false and it does save it when I die but when I equip a Tool in my Hotbar and die the Tool that I had equipped duplicates itself. So I have a Scripts and It’s located in the ServerScriptService this script saves the Tools from my Inventory when I leave the Game

local itemsFolder = game:GetService("ServerStorage"):WaitForChild("Items")
local players = game:GetService("Players")
local dataStoreService = game:GetService("DataStoreService")
local mainStore = dataStoreService:GetDataStore("Main")

local function playerAdded(plr)
	repeat
		task.wait()
	until plr.Character
	task.wait(0.0001)
	local success,Inventory = pcall(function()
		return mainStore:GetAsync(plr.UserId)
	end)
	if success then
		for i,name in pairs(Inventory) do
			local item = itemsFolder:FindFirstChild(name)
			if item then
				local clone = item:Clone()
				clone.Parent = plr.Inventory
			end
		end
	end
end

local function playerLeft(plr)
	local inventory = plr.Inventory
	local char = plr.Character
	local itemsTab = {}
	if char then
		for i,object in pairs(char:GetChildren()) do
			if object and object:IsA("Tool") then
				table.insert(itemsTab,object.Name)
			end
		end
	end
	for i,object in pairs(inventory:GetChildren()) do
		if object and object:IsA("Tool") then
			table.insert(itemsTab,object.Name)
		end
	end
	local success,err = pcall(function()
		return mainStore:SetAsync(plr.UserId,itemsTab)
	end)
	if not success then
		print(err)
	end
end


players.PlayerAdded:Connect(playerAdded)
players.PlayerRemoving:Connect(playerLeft)
1 Like

Or is the maybe another Way???