Leaderstats Wont Go Up When Player Dies

Whenever the attacker shoots the gun, it hits the player. I made a humanoid.died event so that when the person getting shot at dies from the gun, the leaderstats go up. This isn’t the case for some reason. I’m getting no errors from the output so I’m unsure of what’s happening. Any suggestions?

local tool = script.Parent
local remote = tool:WaitForChild("OnShot")

remote.OnServerEvent:Connect(function(player, position, shootpartPos)
	
	local raycastpa = RaycastParams.new()
	raycastpa.FilterDescendantsInstances = {player.Character}
	raycastpa.FilterType = Enum.RaycastFilterType.Blacklist
	
	local raycastresult = workspace:Raycast(shootpartPos, (position - shootpartPos).Unit*3000, raycastpa)
	
	
	if raycastresult then
		local hitpart = raycastresult.Instance
		local model = hitpart:FindFirstAncestorOfClass("Model")
		
		if model then
			if model:FindFirstChild("Humanoid") then
				model.Humanoid.Health -= 5
				print(position)
			end
		end
		if model:FindFirstChild("Humanoid").Died == true then
			local ldrstats = player.leaderstats
			ldrstats.Kills.Value = ldrstats.Kills.Value + 1
		end
	end
end)
4 Likes

Is this for a die counter in the player list

1 Like

It’s for leaderstats on the player list. When the attacked person dies, the kill count is supposed to go up.

1 Like

First off, formatting your code properly may help, performance-wise and also emotionally.

local tool = script.Parent
local remote = tool:WaitForChild("OnShot")

remote.OnServerEvent:Connect(function(player, position, shootpartPos)
	
	local raycastpa = RaycastParams.new()
	raycastpa.FilterDescendantsInstances = {player.Character}
	raycastpa.FilterType = Enum.RaycastFilterType.Blacklist
	
	local raycastresult = workspace:Raycast(shootpartPos, (position - shootpartPos).Unit*3000, raycastpa)
	
	
	if raycastresult then
		local hitpart = raycastresult.Instance
		local model = hitpart:FindFirstAncestorOfClass("Model")
		
		if model then
			humanoid = model:FindFirstChild("Humanoid")
			if humanoid then
				humanoid.Health -= 5
				print(position)
				if humanoid.Health <= 0 then
					player.leaderstats.Kills.Value += 1
				end
			end
		end
	end
end)

Try this.

1 Like

This partially works. When the player dies, it does log the kill on the stats; however, I can keep shooting at the body and the kills will still go up.

1 Like

Oh, my bad. I completely forgot about that. Change the if humanoid.Health <= 0 then line to the original if humanoid.Died == true then

1 Like

I tried this and it now won’t log the kill.

Use ObjectValue.

-- damage script
local tool = script.Parent
local remote = tool:WaitForChild("OnShot")

remote.OnServerEvent:Connect(function(player, position, shootpartPos)

	local raycastpa = RaycastParams.new()
	raycastpa.FilterDescendantsInstances = {player.Character}
	raycastpa.FilterType = Enum.RaycastFilterType.Blacklist

	local raycastresult = workspace:Raycast(shootpartPos, (position - shootpartPos).Unit*3000, raycastpa)


	if raycastresult then
		local hitpart = raycastresult.Instance
		local model = hitpart:FindFirstAncestorOfClass("Model")

		if model then
			humanoid = model:FindFirstChild("Humanoid")
			if humanoid then
				if humanoid.Health > 0 and not humanoid:FindFirstChild("deathreal") then
					local Obj = Instance.new("ObjectValue")
					Obj.Name = "deathreal"
					Obj.Value = player
					Obj.Parent = humanoid
					humanoid.Health -= 5
					print(position)
				end
			end
		end
	end
end)
--- Main script
game:GetService("Players").PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		local Humanoid = Character:FindFirstChildOfClass("Humanoid")
		Humanoid.Died:Connect(function()
			if Humanoid:FindFirstChildOfClass("ObjectValue") then
				local Killer = Humanoid:FindFirstChild("deathreal")
				local Player = Killer.Value
				Player.leaderstats.Kills.Value += 1
				task.wait()
				Killer:Destroy()
			end
		end)
	end)
end)

It won’t deal damage to the player now.

local tool = script.Parent
local remote = tool:WaitForChild("OnShot")
function GiveOBJ(Him: Humanoid?, He:Player?)
	if not Him:FindFirstChild("deathreal") then
		local Obj = Instance.new("ObjectValue")
		Obj.Name = "deathreal"
		Obj.Value = He
		Obj.Parent = humanoid
        game:GetService("Debris"):AddItem(Obj, 10)
	else
		Him:FindFirstChild("deathreal"):Destroy()
		local Obj = Instance.new("ObjectValue")
		Obj.Name = "deathreal"
		Obj.Value = He
		Obj.Parent = humanoid
		game:GetService("Debris"):AddItem(Obj, 10)
	end
end
remote.OnServerEvent:Connect(function(player, position, shootpartPos)

	local raycastpa = RaycastParams.new()
	raycastpa.FilterDescendantsInstances = {player.Character}
	raycastpa.FilterType = Enum.RaycastFilterType.Blacklist

	local raycastresult = workspace:Raycast(shootpartPos, (position - shootpartPos).Unit*3000, raycastpa)


	if raycastresult then
		local hitpart = raycastresult.Instance
		local model = hitpart:FindFirstAncestorOfClass("Model")

		if model then
			humanoid = model:FindFirstChild("Humanoid")
			if humanoid then
				if humanoid.Health > 0 then
					GiveOBJ(humanoid, player)
					humanoid.Health -= 5
					print(position)
				end
			end
		end
	end
end)

Just so you know, .Died() is an event that can be used on a ScriptConnection, and not a boolean.

Humanoid.Died:Connect(function() end)

First


Pretty similar dont you think

Second, Died is not a property. When you do Humanoid.Died, its waiting until the humanoid is dead. For example:

humanoid.Died:Connect(function()
print("heart attack")
end)

This will work. What you’re trying to do will not work. This SHOULD work:

if model:FindFirstChild("Humanoid") then
	model.Humanoid.Died:Connect(function()
		local ldrstats = player.leaderstats
		ldrstats.Kills.Value = ldrstats.Kills.Value + 1
	end)
end

For some reason, it gives me 20 kills instead of 1.

Did my snippet work? I posted a fix for it.

I’m testing that one out now. I’ll get right back to you.

I added your snippet to the top, it didn’t work. It deals damage, but doesnt add onto the leaderstat. I’m getting no errors in the output.

-- damage script
local tool = script.Parent
local remote = tool:WaitForChild("OnShot")

local tool = script.Parent
local remote = tool:WaitForChild("OnShot")
function GiveOBJ(Him: Humanoid?, He:Player?)
	if not Him:FindFirstChild("deathreal") then
		local Obj = Instance.new("ObjectValue")
		Obj.Name = "deathreal"
		Obj.Value = He
		Obj.Parent = humanoid
		game:GetService("Debris"):AddItem(Obj, 10)
	else
		Him:FindFirstChild("deathreal"):Destroy()
		local Obj = Instance.new("ObjectValue")
		Obj.Name = "deathreal"
		Obj.Value = He
		Obj.Parent = humanoid
		game:GetService("Debris"):AddItem(Obj, 10)
	end
end
remote.OnServerEvent:Connect(function(player, position, shootpartPos)

	local raycastpa = RaycastParams.new()
	raycastpa.FilterDescendantsInstances = {player.Character}
	raycastpa.FilterType = Enum.RaycastFilterType.Blacklist

	local raycastresult = workspace:Raycast(shootpartPos, (position - shootpartPos).Unit*3000, raycastpa)


	if raycastresult then
		local hitpart = raycastresult.Instance
		local model = hitpart:FindFirstAncestorOfClass("Model")

		if model then
			humanoid = model:FindFirstChild("Humanoid")
			if humanoid then
				if humanoid.Health > 0 then
					GiveOBJ(humanoid, player)
					humanoid.Health -= 5
					print(position)
				end
			end
		end
	end
end)
--- Main script
game:GetService("Players").PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		local Humanoid = Character:FindFirstChildOfClass("Humanoid")
		Humanoid.Died:Connect(function()
			if Humanoid:FindFirstChildOfClass("ObjectValue") then
				local Killer = Humanoid:FindFirstChild("deathreal")
				local Player = Killer.Value
				Player.leaderstats.Kills.Value += 1
				task.wait()
				Killer:Destroy()
			end
		end)
	end)
end)

Main script section should be in a separate server-script (The one with PlayerAdded)

Oops, I made a minor mistake…
Replace GiveOBJ function with this.

function GiveOBJ(Him: Humanoid?, He:Player?)
	if not Him:FindFirstChild("deathreal") then
		local Obj = Instance.new("ObjectValue")
		Obj.Name = "deathreal"
		Obj.Value = He
		Obj.Parent = Him
		game:GetService("Debris"):AddItem(Obj, 10)
	else
		Him:FindFirstChild("deathreal"):Destroy()
		local Obj = Instance.new("ObjectValue")
		Obj.Name = "deathreal"
		Obj.Value = He
		Obj.Parent = Him
		game:GetService("Debris"):AddItem(Obj, 10)
	end
end
2 Likes

It works! Will this work for any gun as long as I add the damage script to each of the guns?

yes! just put the GiveOBJ function in the script! Also be sure to mark my reply as a solution! (It should look like the current one you have.)

1 Like