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
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)
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)
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?