So basically i have this viewmodel gun script, and whenever you get hit by the bullet you take damage, eveyrhting is fine, until the player you shot dies. If you continue to shoot the corpse it still runs the function because the players health is below 0. I tried using Humanoid.Died but that just made the function fire as many times as you shot the player(ex. I shoot player 3 times, function fires 3 times.) How can I fix this?
function TakeDamage(Player, PlayerDamaged, Damage, BodyPart) if BodyPart.Name ~= "Head" and PlayerDamaged.Humanoid.Health > 0 then PlayerDamaged.Humanoid:TakeDamage(Damage) print(Player.Name .. " hit " .. PlayerDamaged.Name .. " in the " .. BodyPart.Name .. " an dealt " .. Damage .. " damage.") elseif BodyPart.Name == "Head" and PlayerDamaged.Humanoid.Health > 0 then PlayerDamaged.Humanoid:TakeDamage(Damage * 2) print(Player.Name .. " hit " .. PlayerDamaged.Name .. " in the " .. BodyPart.Name .. " an dealt " .. Damage * 2 .. " damage.") end if PlayerDamaged.Humanoid.Health <= 0 then Player.leaderstats.KOs.Value = Player.leaderstats.KOs.Value + 1 warn(Player.Name .. " killed " .. PlayerDamaged.Name .. ".") game.ReplicatedStorage.Notification:FireClient(Player, PlayerDamaged) end end
You will want to add some sort of debounce. That way, once you've awarded points for the death the first time, it wont fire again like you have described.
local Debounce = false --X function TakeDamage(Player, PlayerDamaged, Damage, BodyPart) if BodyPart.Name ~= "Head" and PlayerDamaged.Humanoid.Health > 0 then PlayerDamaged.Humanoid:TakeDamage(Damage) print(Player.Name .. " hit " .. PlayerDamaged.Name .. " in the " .. BodyPart.Name .. " an dealt " .. Damage .. " damage.") elseif BodyPart.Name == "Head" and PlayerDamaged.Humanoid.Health > 0 then PlayerDamaged.Humanoid:TakeDamage(Damage * 2) print(Player.Name .. " hit " .. PlayerDamaged.Name .. " in the " .. BodyPart.Name .. " an dealt " .. Damage * 2 .. " damage.") end if PlayerDamaged.Humanoid.Health <= 0 and Debounce == false then --X Debounce = true --X Player.leaderstats.KOs.Value = Player.leaderstats.KOs.Value + 1 warn(Player.Name .. " killed " .. PlayerDamaged.Name .. ".") game.ReplicatedStorage.Notification:FireClient(Player, PlayerDamaged) end end