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

How can I make it so this part can only damage NPC's once?

Asked by 6 years ago

I have tried running a for loop to check for each part that hit it, and then check to see if they have a humanoid, or in this case, the humanoid called "Enemy." I would like to know if there is a better way to make sure a swing only damages once per slash?

        --//Check for hits\\--
        blade.Touched:Connect(function(hit)
            --Check if we are swinging\\--
            if not swinging then return end

            --Validate humanoid\\--
            for i = 1, hit do
            local hitHumanoid = hit.Parent:FindFirstChild("Enemy")
            if hitHumanoid then


                if canDamage then
                    --//Damaage Humanoid\\--



                    hitHumanoid[i]:TakeDamage(math.random(mindmg.Value,maxdmg.Value))
                    canDamage = false
                    local tag = Instance.new("ObjectValue")
                    tag.Name = "creator"
                    tag.Parent = hitHumanoid[i]
                    tag.Value = player
                    wait(8)
                    tag:Destroy()
                    end



                end

                end
        end)
0
You need to check for the tag if it already has one don't do damage. Lord_CthuIhu 2 — 6y

2 answers

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

I can think of 2 ways to do this.

You could make a blank table and whenever it damages a humanoid add the humanoids parents name to the array, then before damaging check if the humanoid parents name is in the table.

local intable = false
for i, v in ipairs(MyTable) do
    if v == humanoid.Parent.Name then
        intable = true
    end
end
if intable = false then
    humanoid:TakeDamage()
    table.insert(MyTable, humanoid.Parent.Name)
end

downside to this is the humanoid will not be able to be hurt again until the script is reset. and if 2 humanoids have the same name then they both wont be hurt anymore.

Or when the humanoid is damaged add some object to the humanoid and name it hurt then check if it there before hurting it like this

local time = 100--the amount of seconds until the humanoid can be hurt again

if humanoid:FindFirstChild("Hurt") == nil then
    humanoid:TakeDamage()
    local hurt = Instance.new("BoolValue",humanoid)
    hurt.Name = "Hurt"
    game.Debris:AddItem(hurt,time)
end

If you want them to not be hurt again until they reset then just remove game.Debris:AddItem(hurt,time)

number 2 is the best option unless you only want them to be hurt once and never again then you would use option 1

0
Thank you very much, Option 2 was the one I was trying to go for :) kakikito123 4 — 6y
Ad
Log in to vote
0
Answered by 6 years ago

What you want to do is declare canDamage as true at the top of the script outside of the function. As soon as the blade as touched a part that is a character, make canDamage = false.

This way it wont damage anything else until you set canDamage back to true (which should be set after a wait(#) time.

canDamage = true

Blade.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild('Enemy') then -- check to see if it was a person with a humanoid
        local humanoid = hit.Parent.Enemy
        if canDamage == true then -- will only damage if canDamage is true
            canDamage = false -- set it to false so it wont damage again right away
            humanoid:TakeDamage(math.random(mindmg.Value,maxdmg.Value)
            wait()--insert how long you want it to wait before it can damage again
            canDamage = true
end)
0
The thing is, if there are two or more enemies together, it only damages whoever was the first to touch the blade, but not the rest of the mob. How do you think I can work that? kakikito123 4 — 6y

Answer this question