1 Kill = 1 KO, atleast that's what it's supposed to be

Hello devforum,
My leaderboard value is jumping up to insane values when I kill something, when it should only increment by 1 for each kill.

I know that this is due to me putting my leaderboard update in the touched loop, but in order to record a KO I need a way to see whether the humanoid that the bullet touches has died (Unless there is a better way to count them)

MY CODE:
local bullet = script.Parent
local creator = script.Parent.Player.Value

local function touched(part)
	local function award()
		print("e")	
	end
	part.Parent.Humanoid.Died:connect(award)
	print("1", script.Parent.Parent)
	print("2", part)
	if part.Parent == workspace
	then
		print("INVALID TARGET")
	else
		if part.Parent.Humanoid ~= nil
		then
			if tostring(part.Parent) == tostring(creator)
			then
				print("TARGET IS TOOLUSER")
			else	
				part.Parent.Humanoid.Health = part.Parent.Humanoid.Health - 5
			end
		end
	end
end

bullet.Touched:connect(touched)

I need a way to see whether a humanoid has died to update the value by 1, without putting it in the touched loop

Each time the bullet hits a character with a humanoid, a connection to the humanoid’s Died event is connected. This means that if you shoot 5 bullets at a character and the fifth one kills it, then all 5 of the humanoid.Died signals will fire, increasing the kill count by 5 rather than 1.

1 Like

To add KOs for when a player dies you can simply just make a CharacterAdded event, and connect a Died function then.

e.g

game.Players.PlayerAdded:Connect(function(Player)
Player.CharacterAdded:Connect(function(Character)
local Humanoid = Character:WaitForChild("Character")
Humanoid.Died:Connect(function()
Player.leaderstats.NAMEHERE.Value = Player.leaderstats.NAMEHERE.Value + 1
end)
end)
end)

Wouldn’t this make it so that someone respawning and then dying somewhere on the map gives a player somewhere a KO? characteradded is for when players respawn, and I want to get a died signal from the character that the bullet touched

Then add a tag inside of the humanoid when it touches them. The tag would either be an objectvalue or a stringvalue. Then when the died function runs you check the humanoid of the player, and then get the player through game.Players and give them their KOs.

this is an example but this is how it would be

-- add a player variable here
bullet.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
Instance.new("ObjectValue",hit.Parent.Humanoid).Value = player
end
end)

also just letting you know you need to check if there isn’t already an objectvalue with the same value before adding it. it’s pretty simple to do.

1 Like

After removing 5 from the humanoid’s health, you can check if the health is 0. If it is, then call award().

I did that, and it lead to the same problem of having more than 1 KO

It may be that the touched functions is running multiple times, try adding debounce variable so that it only awards it once. Bad explaining, but I hope you get what I meant.