The kill counter still increases after you keep attacking the dead player

when I kill the character with a sword, I want the number of kills to increase by 1

after the player dies, if you keep hitting, he gives more kills how can I fix this problem?
record:

script:

script.Parent.Handle.Touched:Connect(function(touch)

	if script.Parent.CanDamage.Value == true then

		local humanoid = touch.Parent:FindFirstChild("Humanoid")

		if not touch.Parent:FindFirstChild("Humanoid") then 

			humanoid = touch.Parent.Parent:FindFirstChild("Humanoid")

		end

		if humanoid.Name ~= "Humanoid" then return end
		
		script.Parent.CanDamage.Value = false

		humanoid:TakeDamage(20)
		
		if humanoid.Health < 1 then
			
			local plr = game.Players:GetPlayerFromCharacter(script.Parent.Parent)
			plr.leaderstats.Kills.Value = plr.leaderstats.Kills.Value + 1
			
		end
		
		wait(1)
		
		script.Parent.CanDamage.Value = true
	end
end)
1 Like

you could try to change your:
humanoid.Health < 1
to:
humanoid.Health > 0

this makes it so that it only adds when their health is above 0 rather than adding if their health is below 1. checking if their health is below one results in the script adding 1 to the leader stats since the humanoid you hit has a health below 1.

Now it counts every hit as a kill

OOOOH I’M SO SORRY I FORGOT ABOUT THAT.

you can put a check before adding 1 kill to ensure they are not already dead. It would be something like this:

if humanoid.Health < 1 then
			local plr = game.Players:GetPlayerFromCharacter(script.Parent.Parent)
			if plr.Character.Humanoid.Health > 0 then -- new check. if their health is above 0 then give opponent a kill.
				plr.leaderstats.Kills.Value = plr.leaderstats.Kills.Value + 1
			end
		end

You would need to set up .Died connections of players characters

so where a player .Died, you can award the killer with a kill, you would also have to make a tag system so that on hit, the object value of the hitter (killer) is stored.

of course, you could just check if the incoming damage would kill the target, then add a kill and kill the target then

i.e.

if Humanoid.Health<=0 then return end

--if incoming damage is lethal
if Humanoid.Health-20 <=0 then
--add to killstat
end

Humanoid:TakeDamage(20)

when you’re dead, the kill counter is increasing

can you make died connections i don’t know much coding :d

just use that, it would be the easiest way for you based on your coding experience

script.Parent.Handle.Touched:Connect(function(touch)

	if script.Parent.CanDamage.Value == true then

		local humanoid = touch.Parent:FindFirstChild("Humanoid")

		if not touch.Parent:FindFirstChild("Humanoid") then 

			humanoid = touch.Parent.Parent:FindFirstChild("Humanoid")

		end

		if humanoid.Name ~= "Humanoid" then return end

		script.Parent.CanDamage.Value = false
		
		if humanoid.Health <= 0 then return end
			
		if humanoid.Health-20 <= 0 then

			local plr = game.Players:GetPlayerFromCharacter(script.Parent.Parent)
			plr.leaderstats.Kills.Value = plr.leaderstats.Kills.Value + 1

		end
		
		humanoid:TakeDamage(20)

		wait(1)

		script.Parent.CanDamage.Value = true
	end
end)

Not working

if humanoid.Health < 1 and humanoid:FindFirstChild("Killed") == nil then
    local killedTag = Instance.new("BoolValue")
    killedTag.Name = "Killed"
    killedTag.Parent = humanoid
    local plr = game.Players:GetPlayerFromCharacter(script.Parent.Parent)
	plr.leaderstats.Kills.Value = plr.leaderstats.Kills.Value + 1
end

I get error Players.D0guhanKHN.Backpack.ClassicSword.DamageScript:24: attempt to index nil with ‘leaderstats’

Do this and tell me what it prints in the output

if humanoid.Health < 1 and humanoid:FindFirstChild("Killed") == nil then
    local killedTag = Instance.new("BoolValue")
    killedTag.Name = "Killed"
    killedTag.Parent = humanoid
print(script.Parent)
print(script.Parent.Parent)
print(script.Parent.Parent.Parent)
    local plr = game.Players:GetPlayerFromCharacter(script.Parent.Parent)
	plr.leaderstats.Kills.Value = plr.leaderstats.Kills.Value + 1
end

How was it not working? Logically, the code is sound. Any errors?

try this:

Dead = false
script.Parent.Handle.Touched:Connect(function(touch)

	if script.Parent.CanDamage.Value == true and not Dead then

		local humanoid = touch.Parent:FindFirstChild("Humanoid")

		if not touch.Parent:FindFirstChild("Humanoid") then 

			humanoid = touch.Parent.Parent:FindFirstChild("Humanoid")

		end

		if humanoid.Name ~= "Humanoid" then return end
		
		script.Parent.CanDamage.Value = false

		humanoid:TakeDamage(20)
		
		if humanoid.Health < 1 then
			Dead = true
			local plr = game.Players:GetPlayerFromCharacter(script.Parent.Parent)
			plr.leaderstats.Kills.Value = plr.leaderstats.Kills.Value + 1
			
		end
		
		wait(1)
		
		script.Parent.CanDamage.Value = true
	end
end)
script.Parent.Handle.Touched:Connect(function(touch)
	if script.Parent.CanDamage.Value then
		local humanoid = touch.Parent:FindFirstChild("Humanoid")
		if not humanoid then
			humanoid = touch.Parent.Parent:FindFirstChild("Humanoid")
		end
		if not humanoid or humanoid.Name ~= "Humanoid" then
			return
		end

		script.Parent.CanDamage.Value = false
		humanoid:TakeDamage(20)

		if humanoid.Health <= 0 then
			local plr = game.Players:GetPlayerFromCharacter(script.Parent.Parent)
			if plr then
				local leaderstats = plr:FindFirstChild("leaderstats")
				if leaderstats then
					local kills = leaderstats:FindFirstChild("Kills")
					if kills and kills:IsA("IntValue") then
						kills.Value = kills.Value + 1
					end
				end
			end
		end

		wait(1)

		script.Parent.CanDamage.Value = true
	end
end)

i made some differences using your code and it worked thank you

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