Headshots not working

Hello!

I made a gun script that damages a player when hit. The problem is that it dosen’t work with headshots. I don’t know what’s going on with my script. Help is appreciated!

local bullet = script.Parent

local debounce = false

bullet.Touched:Connect(function(hitpart)
	local player = game.Players:GetPlayerFromCharacter(hitpart.Parent)
	local Attacker = hitpart.Parent:FindFirstChild("Attacker")
	    if player and Attacker.Value ~= player.Name then
				if debounce == false then
					debounce = true
					hitpart.Parent.Humanoid:TakeDamage(30)
					wait(1)
			debounce = false
			
		elseif player and Attacker.Value ~= player.Name and hitpart.Name == "Head" then
			if debounce == false then
				debounce = true
				hitpart.Parent.Humanoid:TakeDamage(100)
				wait(1)
				debounce = false
		end
	end
end
end)

Well if you hit their head, the first if statement will trigger, therefore the elseif statement won’t trigger. For the first if statement, make sure that the hit part isn’t the head. Replace this line:

with this:

if player and Attacker.Value ~= player.Name and hitpart.Name ~= "Head" then

It dosen’t detect the head, only the body.

So you mean if you hit them in the head it doesn’t even deal 30 damage?

Nope, it only damages the body, it dosen’t even damage the player when the bulllet hits the head.

Very strange, and no errors?___

local bullet = script.Parent

local debounce = false

bullet.Touched:Connect(function(hitpart)
	local player = game.Players:GetPlayerFromCharacter(hitpart.Parent)
	local Attacker = hitpart.Parent:FindFirstChild("Attacker")
	    if player and Attacker.Value ~= player.Name and hitpart.Name ~= "Head" then -- line 8
				if debounce == false then
					debounce = true
					hitpart.Parent.Humanoid:TakeDamage(30)
					wait(1)
			debounce = false
			
		elseif player and Attacker.Value ~= player.Name and hitpart.Name == "Head" then
			if debounce == false then
				debounce = true
				hitpart.Parent.Humanoid:TakeDamage(100)
				wait(1)
				debounce = false
		end
	end
end
end)

check line 8 :confused:

I fixed it, but the body is not getting damaged now.

local bullet = script.Parent

local debounce = false

bullet.Touched:Connect(function(hitpart)
	local player = game.Players:GetPlayerFromCharacter(hitpart.Parent)
	local Attacker = hitpart.Parent:FindFirstChild("Attacker")
	if player and Attacker.Value ~= player.Name and hitpart.Name == "Head" then
		if debounce == false then
			print("headshot")
					debounce = true
					hitpart.Parent.Humanoid:TakeDamage(100)
					wait(1)
			debounce = false
		else
			if debounce == false then
				debounce = true
				hitpart.Parent.Humanoid:TakeDamage(30)
				wait(1)
				debounce = false
		end
	end
end
end)

This line should use ~= at the end.

if player and Attacker.Value ~= player.Name and hitpart.Name ~= "Head" then

The whole body dies, and the head dosent do any damage.

Go back to your original script, that one is the most correct. Try adding prints like hitpart and hit part.Parent.

It prints the part thats hit, but no headshot damage, only regular body shots.

So when it hits the head does it print “Head”?

Yes, but it only does body shots, not headshots,

Does it print multiple things per shot? Maybe the debounce causes it to not work?

Yeah, multiple things. I only put the debounce so it can’t insta-kill the player at body shots.

You could try deleting the bullet after it has touched something. That could replace the debounce.

Sorry for asking so many questions, but it helps for me to figure out the problem. When it hits a body part does it print multiple things too, or only for headshots?

No, it also prints “Baseplate”, and the gun tool itself.

Idk then :frowning: . No errors or anything?