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

How would I implement an anti-team kill feature into my script?

Asked by
Duksten 20
7 years ago
Edited 7 years ago

I have this script for a hand axe that I'm making for a game, so far it works perfectly fine. Each time it hits a humanoid it deals damage. However I also want it so that it wont deal any damage to any of the player's team mates, and I have tried using GetPlayerFromCharacter however I am having trouble on figuring out how to use it in my code when the script detects that a humanoid has hit the handle.

Here is the snippet of my code that fixates on hit detection.

local canDamage = true
canChop = true
Tool.Handle.Touched:connect(function(hit)
    if not swinging then return end
    if not canDamage or not canChop then return end

    function damage()
        canDamage = false
        hit.Parent:FindFirstChild("Humanoid"):TakeDamage(26)
        Tool.Handle.FleshHit:Play()
        wait(coolDown2)
        canDamage = true
    end

    local function chop()
        canChop = false
        local chopValue = hit.Parent.ChopValue
        local addValue = 1
        chopValue.Value = chopValue.Value - addValue
        Tool.Handle.Chop:Play()
        wait(coolDown2)
        canChop = true
    end

    if hit and hit.Parent:FindFirstChild("Humanoid") then

        damage()

    elseif hit and hit.Parent:FindFirstChild("ChopValue") then --if hit tree then

        chop()

    end
end)


0
No need to do "var == false", or "var == true", you can use "var" and "not var". Programical 653 — 7y
0
thanks made some few minor edits to the code that i posted Duksten 20 — 7y

2 answers

Log in to vote
0
Answered by
gitrog 326 Moderation Voter
7 years ago

Alright, I'm going to give you the code for it. So, you'll need to check TeamColors when it does damage. We can do this using GetPlayerFromCharacter. Before the Humanoid takes damage, you'll want to check the player's TeamColor.

 function damage()
        canDamage = false
    if hit.Parent:FindFirstChild("Humanoid") then
        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
        local user = game.Players:GetPlayerFromCharacter(Tool.Parent)
        if player then
            if player.TeamColor == user.TeamColor then
                --penalties for teamkilling
            else
                hit.Parent.Humanoid:TakeDamage(26)
            end
        end
    end
        Tool.Handle.FleshHit:Play()
        wait(coolDown2)
        canDamage = true
    end
Ad
Log in to vote
-1
Answered by 7 years ago

Before your damage script you can check if the person you are hitting is the same TeamColor or not, if they are then make damage 0, if not make the axe do 26 damage.

0
Good job explaining.. bad job providing what you explain in code. Goulstem 8144 — 7y
0
Maybe he could have elaborated more, but I'm not sure the dude should've had to code for him. AdministratorReece 193 — 7y
0
I'm not going to code for him. Shadowthaumaturge 97 — 7y

Answer this question