The issue is Humanoid.Died doesn’t fire at first death.
This script is about saving inventory with DataStoreService. load when you join the game, load every time the character respawns, save when the player dies, and save when the player leaves the game.
How it works:
(check data, then check first storage and load, then second storage and load)
It doesn’t save when I reset the character for the first time, but it saves normally after the first reset.
Server-Sided Script:
local DataStoreService = game:GetService("DataStoreService");
local ServerStorage = game:GetService("ServerStorage");
local Players = game:GetService("Players");
local ItemStorage_1 = ServerStorage:FindFirstChild("ItemStorage_1");
local ItemStorage_2 = ServerStorage:FindFirstChild("ItemStorage_2");
local ItemData = DataStoreService:GetDataStore("ItemData");
local function onPlayerAdded(Player : Player)
local Key = "Player_"..Player.UserId;
local SavedTool;
local Success, Error = pcall(function()
SavedTool = ItemData:GetAsync(Key) or {};
end)
if Success then
for _, Tool in ipairs(SavedTool) do
local CheckTool = ItemStorage_1:FindFirstChild(Tool);
print(Tool)
if CheckTool then
local ClonedTool = CheckTool:Clone();
ClonedTool.Parent = Player.Backpack;
end
end
for _, Tool in ipairs(SavedTool) do
local CheckTool = ItemStorage_2:FindFirstChild(Tool);
if CheckTool then
local ClonedTool = CheckTool:Clone();
ClonedTool.Parent = Player.Backpack;
end
end
Player.CharacterAdded:Connect(function(Character)
local Humanoid = Character:FindFirstChildOfClass("Humanoid");
repeat task.wait() until Character;
local NewSavedTool;
local Success, Error = pcall(function()
NewSavedTool = ItemData:GetAsync(Key) or {};
end)
for _, Tool in ipairs(NewSavedTool) do
local CheckTool = ItemStorage_1:FindFirstChild(Tool);
print(Tool)
if CheckTool then
local ClonedTool = CheckTool:Clone();
ClonedTool.Parent = Player.Backpack;
end
end
for _, Tool in ipairs(NewSavedTool) do
local CheckTool = ItemStorage_2:FindFirstChild(Tool);
if CheckTool then
local ClonedTool = CheckTool:Clone();
ClonedTool.Parent = Player.Backpack;
end
end
repeat task.wait() until Humanoid
Humanoid.Died:Connect(function()
Humanoid:UnequipTools()
local TempSaved = {};
for _, Tool in ipairs(Player.Backpack:GetChildren()) do
table.insert(TempSaved, Tool.Name);
end
local Success, Error = pcall(function()
ItemData:UpdateAsync(Key, function(OldData)
return TempSaved;
end)
end)
end)
end)
else
Player:Kick("Something went wrong...");
end
end
local function onPlayerRemoving(Player : Player)
local Key = "Player_"..Player.UserId;
local OwnedTool = {};
for _, Tool in ipairs(Player.Backpack:GetChildren()) do
table.insert(OwnedTool, Tool.Name);
end
local Success, Error = pcall(function()
ItemData:SetAsync(Key, OwnedTool);
end)
if Success then
print("Saved Inventory!");
else
print("Something went wrong...");
end
end
Players.PlayerAdded:Connect(onPlayerAdded);
Players.PlayerRemoving:Connect(onPlayerRemoving);
for i, v in ipairs(Players:GetChildren()) do
task.spawn(onPlayerAdded, v);
end
Please help me fix this problem and give an example, then tell me the issue with this script so I can improve myself.