Humanoid.Died doesn't fire at first death

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.

I notice this pattern with Humanoid, same with Character.

local Humanoid = Character:FindFirstChildOfClass("Humanoid");

repeat task.wait() until Humanoid;

Variables will not change unless you change them. If this gets nil it will only every be nil, it does not re-run :FindFirstChildOfClass Try to work with Character:WaitForChild("Humanoid"). If this doesn’t solve your error it will at least make your script run two frames faster.

1 Like

Thank you for the advice; however, it does not work, but ok.

I found the solution; it’s because the characters load into the game faster than the player.CharacterAdded script setup here the updated script anyone can use it:

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;
	
	Player.CharacterAdded:Connect(function(Character)
		repeat task.wait() until Character
		local Humanoid = Character:WaitForChild("Humanoid");

		local NewSavedTool;

		local Success, Error = pcall(function()
			NewSavedTool = ItemData:GetAsync(Key) or {};
		end)

		Humanoid.Died:Connect(function()
			Humanoid:UnequipTools()
			local TempSaved = {};

			for _, Tool in ipairs(Player.Backpack:GetChildren()) do
				table.insert(TempSaved, Tool.Name);
			end

			print("from tempsave: ", TempSaved)

			local Success, Error = pcall(function()
				ItemData:UpdateAsync(Key, function(OldData)
					return TempSaved;
				end)
			end)
		end)

		for _, Tool in ipairs(NewSavedTool) do
			local CheckTool = ItemStorage_1:FindFirstChild(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
	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
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.