Humanoid.died event working perfectly fine on player reset but not when killed by an npc

hello! i know this has happened to other players but this is truly confusing.

i have 2 scripts that are linked to each other.

server script:

local rs = game:GetService("ReplicatedStorage")
local ss = game:GetService("ServerStorage")
local classSelect = rs:WaitForChild("ClassSelect")
local items = ss.Items
local classGiveOnDeath = rs:WaitForChild("ClassGiveOnDeath")


classSelect.OnServerEvent:Connect(function(plr, classID)
	print(plr, classID)
	
	wait(2)
	
	local char = plr.Character
	local h = char:WaitForChild("Humanoid")
	local TempClassID = char:WaitForChild("Class")
	local class = classID
	
	local selectedClass = tostring(class)
	
	print(char, selectedClass)
	
	if selectedClass == "Slicer" then
		local clonedItem = items.Slicer:Clone()
		clonedItem.Parent = plr.Backpack
		h.WalkSpeed = 16
		TempClassID.Value = "Slicer"
		classGiveOnDeath:FireClient(plr, clonedItem)
	end

	if selectedClass == "Crusher" then
		local clonedItem = items.Crusher:Clone()
		clonedItem.Parent = plr.Backpack
		h.WalkSpeed = 10
		TempClassID.Value = "Crusher"
		classGiveOnDeath:FireClient(plr, clonedItem)
	end

	if selectedClass == "Baller" then
		local clonedItem = items.Baller:Clone()
		clonedItem.Parent = plr.Backpack
		h.WalkSpeed = 28
		TempClassID.Value = "Baller"
		classGiveOnDeath:FireClient(plr, clonedItem)
	end

	if selectedClass == "Piercer" then
		local clonedItem = items.Piercer:Clone()
		clonedItem.Parent = plr.Backpack
		h.WalkSpeed = 22
		TempClassID.Value = "Piercer"
		classGiveOnDeath:FireClient(plr, clonedItem)
	end
end)

local script:

local rs = game:GetService("ReplicatedStorage")
local event = rs:WaitForChild("ClassGiveOnDeath")
local event2 = rs:WaitForChild("ClassSelect")
local ClassID = script.Parent.Value
local player = game.Players.LocalPlayer

local function setClassID(clonedItem)
	
	print(clonedItem)
	local stringItem = tostring(clonedItem)
	print(stringItem)
	
	ClassID = stringItem
	
	print(ClassID)
	
	return ClassID
end









game.Players.LocalPlayer.CharacterAdded:Connect(function()
	local char = player.Character
	char:WaitForChild("Humanoid").Died:Connect(function()
		wait(2)
		local classID = ClassID
		print(classID)
		event2:FireServer(classID)
		print("Player died")
	end)
end)


event.OnClientEvent:Connect(setClassID)

I have checked other people’s dilemmas similar to mine but they do not work.

this humanoid.died event runs ONLY when the player resets, and not when an npc kills the player.

Where is the script located?
And does another player killing the player with a weapon fire it?

1 Like

i have same questions as @BriefYayyayyay but i also wanna know how the npcs deal dmg to the players

also i think the CharacterAdded event passes the new character model as a parameter so you dont need to do player.Character. You should try using the passed value and see if anything changes.

script is located in serverscriptservice, and local script is located in starterplayerscripts under a stringValue. The npcs deal damage when you get close to them with :TakeDamage

And also yes, it does fire when another player kills you

Put the localscript in startercharacterscripts, so it can run when the player respawns.
I’m not sure if this will fix the main issue, though.
Edit: And why do you have a playeradded function?
It’s a localscript, so you should be able to use game.Players.LocalPlayer

so i moved it to startercharacterscripts (the local script) and it still doesn’t work when the npc kills you

alright fixed it, had the .died event run in the script and send a remote event to the local script in starterplayerscripts, which then sent the signal back to it.