Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by
Galicate 106
5 years ago
Edited 5 years ago

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
0
Keep in mind that you don't *fire* functions, you *call* them. avozzo 22 — 5y
0
Basically you need to tell the script if the player is dead Vexuss_Recon -19 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

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
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! SnowFunsize 55 — 5y
Ad

Answer this question