Problems with detecting player/ npc death

So i made an attack, everything seems to be working, except the death detection, tried creating an objectvalue, I also have another script that has the leaderstats and detecting if player died. Not sure what I did wrong, the output is not printing any errors.

local rs = game:GetService("ReplicatedStorage")
local Attackevent = rs:WaitForChild("Attackevent")
local attack = game.ServerStorage:WaitForChild("Electroball")
local dmged = false

Attackevent.OnServerEvent:Connect(function(plr)
	local char = plr.Character
    local hum = plr:FindFirstChild("Humanoid")

	local rp = char.PrimaryPart
	local newattack = attack:Clone()
	newattack.Parent = game.Workspace
	newattack.CanCollide = false
	newattack.Anchored = false
	newattack.Position = rp.CFrame.Position + rp.CFrame.LookVector
    
    
	local bv = Instance.new("BodyVelocity")
	bv.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
	bv.Velocity = rp.CFrame.LookVector * 100
	bv.Parent = newattack
    
	game.Debris:AddItem(newattack, 5)
	
	newattack.Touched:Connect(function(hit)
		local humanoid = hit.Parent:FindFirstChild("Humanoid")

		if humanoid and plr.Name ~= hit.Parent.Name then
			humanoid:TakeDamage(30)
		end
	end)
	
	
	newattack.Touched:Connect(function(Hit)
		local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
		if Player then
			local Tagged = Instance.new("ObjectValue")
			Tagged.Name = "creator"
			Tagged.Value = Player
			Tagged.Parent = Hit.Parent.Humanoid
			game.Debris:AddItem(Tagged, 2)
		end
	end)

end)

the other piece of code is

game.Players.PlayerAdded:Connect(function(Player)

	Player.CharacterAdded:Connect(function(Character)
		Character.Humanoid.Died:Connect(function()
			if Character.Humanoid:FindFirstChild("creator") then
				local Player = Character.Humanoid.creator.Value
				local Leaderstats = Player.leaderstats
				local kills = Leaderstats.Kills
				Kills.Value = kills.Value + 1
			end
		end)
	end)
end)
1 Like

try not to connect two functions to the Touched event.
They might not be running in the correct order,
for example, it could be 1. the humanoid take damage and died immediately, 2. leader stat tries to get object value but there isn’t one 3. the attack created the object value

if all of them happen on server side, i would instead do all the things in the specific order within Touched

	newattack.Touched:Connect(function(hit)
		local humanoid = hit.Parent:FindFirstChild("Humanoid")

		if humanoid and plr.Name ~= hit.Parent.Name then
			humanoid:TakeDamage(30)
			if humanoid.Health <= 0 and humanoid:GetState() ~= Enum.HumanoidStateType.Dead then
				local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
				if Player then
					Player.leaderstats.Kills.Value += 1
				end
			end
		end
	end)

Hey thanks for the help, I was just wondering if there was anyway to detect who killed an npc with specifically an objectValue?

you could add the object value like you did
but add it right after humanoid take damage , with a health and state check like i did

you may then still running into racing condition (Died before object value is added)

in that case, because it should now only have one such object value added because of the health and state checks,
you can use ChildAdded instead of Died event