DataStore function when Player dies?(I already did it when player leaves)

Hi everyone I’m currently working on an Inventory System and rn I have a System that saves my Stuff(Tools)in the InventoryFrame when Humanoid leaves the Server and like u guys red in the Title or sth. I want to let it save when I die cuz I set ResetOnDeath to false and it saves but it duplicates my Items so I just need to change the Script ima show u know from leaving to dieing or reseting or I need a Code that stops letting the ScreenGui Copy Tools but the ScreenGui only saves when I equip a tool in my Hotbar(Backpack) and die(Here’s the Script)

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)

What is player.Inventory? You probably should give us more details since we do not know what exactly you mean.

1 Like

well plr.Inventory is the Place where the Tools are or sth. for example If I put Backpack instead of Inventory the Tools in the Hotbar will be saved but the ones in the InventoryFrame.(Here a picture)


This is the InventoryPlace and ye…idk did I explain it good?

Do you mean the player’s backpack (Player.Backpack)?

1 Like

I mean with player’s backpack (Player.Backpack) ye, but in this situation I want (Player.Inventory)

I am still not sure what exactly you need, do you need the gui to update or the items to save?

I need the Items to save when I die like they shouldn’t duplicate themself in the Inventory when I equip the Tool in my Hotbar and Die I just need it to save

Don’t use the player backpack to save things, use the player’s startergear instead of the backpack and make sure you clone the tools into the backpack and the startergear.

2 Likes

i want Plr.Inventory not Backpack because I want the Tools in the inventory Frame to be saved and I just want to change the ,local function playerLeft(plr)" function and the local function playerAdded(plr) function to if Humanoid Dies or sth. I just need to change abit of the Code so it saves my Tools when I die not when I leave I just need to change this so I have a Script for Leaving and Dieing

Startergear saves the tools even when you die, and then you just need one script that saves the tools in startergear when you leave.

1 Like

I changed it to starterGear but when I did it and died the Tools appeared in my Hotbar and not in my inventory