Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How can I keep the function from firing in the player is dead?

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

2 comments

0

Keep in mind that you don't fire functions, you call them.

0

Basically you need to tell the script if the player is dead

1 answer

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

1 comment

0

I've also gone ahead and added a "--X" next to each line that I have altered to help point it out to you!

This discussion is preserved as it was. New answers, comments, and votes are closed.