Shooting a dead player with gun still gives points?

I am making a bounty system where when you kill a player, that players points will be awarded to you, plus 20 points. If that makes sense.

When I was testing it out, it worked as expected except for the fact that it also awards points when shooting a player that is dead.

Here is the part of my code which awards the points (everything is already defined):

if hit then
	if hit.Parent:FindFirstChild("Humanoid") and hit.Parent.Name ~= tool.Parent.Name then
		if debounce == true then 
        debounce = false
	    hit.Parent.Humanoid:TakeDamage(8)
		if hit.Parent.Humanoid.Health <= 0 then
		    local killerplayer = game.Players:GetPlayerFromCharacter(tool.Parent)
		    local killedplayer = game.Players:GetPlayerFromCharacter(hit.Parent)
		    if killerplayer:FindFirstChild("leaderstats") and killerplayer.leaderstats:FindFirstChild("Bounty") and killedplayer:FindFirstChild("leaderstats") and killedplayer.leaderstats:FindFirstChild("Bounty") then
			    local killerbounty = killerplayer.leaderstats.Bounty
			    local killedbounty = killedplayer.leaderstats.Bounty
			    killerbounty.Value = killerbounty.Value + (20 + killedbounty.Value)
			    killedbounty.Value = 0
            end
        end
		wait(0.05)
		debounce = true
		end
	end
end

This is a raycast gun, by the way.

1 Like

You’re damaging the player and then checking afterwards if their health is below 0 so you can award points. Change the script so that you check before damaging if they are already dead, then damage them, then check if they are currently dead, and award points if they are.

Is this what you meant?

if hit.Parent:FindFirstChild("Humanoid") and hit.Parent.Name ~= tool.Parent.Name then
	if debounce == true then 
		debounce = false
		if hit.Parent.Humanoid.Health > 0 then
			hit.Parent.Humanoid:TakeDamage(8)
		else end
		wait(0.05)
		debounce = true
	end
		
	if hit.Parent.Humanoid.Health <= 0 then
		local killerplayer = game.Players:GetPlayerFromCharacter(tool.Parent)
		local killedplayer = game.Players:GetPlayerFromCharacter(hit.Parent)
		if killerplayer:FindFirstChild("leaderstats") and killerplayer.leaderstats:FindFirstChild("Bounty") and killedplayer:FindFirstChild("leaderstats") and killedplayer.leaderstats:FindFirstChild("Bounty") then
			local killerbounty = killerplayer.leaderstats.Bounty
			local killedbounty = killedplayer.leaderstats.Bounty
			killerbounty.Value = killerbounty.Value + (20 + killedbounty.Value)
			killedbounty.Value = 0
		end
	end
end

If so, it’s still giving you points when you shoot a dead player.

Honestly, the method of approaching this is kind of poor. The if statement for health being <= 0 will fire every time you shoot them after they die, because the condition is being met.

The best way to do this is to create a tagging system; when a player gets hit, they get “tagged” with the player who hit them, and then set up a listener for humanoid.Died that gives the bounty to the last tagged person, if one exists.

1 Like

I had a bit of a revelation after I tabbed out, and came back to correct myself: while a tagging system would be more versatile for the future, this should work for now:

I added an initial check when the player is hit to make sure they aren’t already dead before executing any code

if hit then
	if hit.Parent:FindFirstChild("Humanoid") and hit.Parent.Name ~= tool.Parent.Name then
		if hit.Parent.Humanoid.Health > 0 then
			if debounce == true then 
			debounce = false
			hit.Parent.Humanoid:TakeDamage(8)
			if hit.Parent.Humanoid.Health <= 0 then
				local killerplayer = game.Players:GetPlayerFromCharacter(tool.Parent)
				local killedplayer = game.Players:GetPlayerFromCharacter(hit.Parent)
				if killerplayer:FindFirstChild("leaderstats") and killerplayer.leaderstats:FindFirstChild("Bounty") and killedplayer:FindFirstChild("leaderstats") and killedplayer.leaderstats:FindFirstChild("Bounty") then
					local killerbounty = killerplayer.leaderstats.Bounty
					local killedbounty = killedplayer.leaderstats.Bounty
					killerbounty.Value = killerbounty.Value + (20 + killedbounty.Value)
					killedbounty.Value = 0
				end
			end
			wait(0.05)
			debounce = true
			end
		end
	end
end
1 Like

Thanks! It works. :grin:

I am going to use a tagging system for another thing I am working on, so I might use the tagging idea in the future.

1 Like