DataStore functions when player dies(Easy)?

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 is there a way we can change this script to if player died or we can stop the ScreenGui from duplicating?(I heard someone told me bout putting the ScreenGui in the ReplicatedStorage and parent it or sth. but I didn’t really understand!)

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)