#EnemyAI keeps friendly firing itself

1 messages · Page 1 of 1 (latest)

wise sinew
#

In this enemyai attack function for some reason, even though i pass an "if player" check, if any other enemy walks into the attack hitbox, they'll damage their own. How do i solve this?

#

wont lemem send the function holdo n

#
    local humanoid = enemy:FindFirstChild("Humanoid")
    local hrp = enemy:FindFirstChild("HumanoidRootPart")
    --local targetHum = target:FindFirstChild("Humanoid")
    local ATTACK_COOLDOWN = enemy:GetAttribute("Cooldown")
    local ATTACK_DAMAGE = enemy:GetAttribute("Damage")
    local ATTACK_OFFSET = 4
    local HITBOX_SIZE = enemy:GetAttribute("Hitbox")

    local lastAttack = enemy:GetAttribute("LastAttack") or 0
    if tick() - lastAttack >= ATTACK_COOLDOWN then
        enemy:SetAttribute("LastAttack", tick())
        -- reset the timer
        
        local hitbox = Instance.new("Part")
        hitbox.Size = Vector3.new(1, 1, 1) * HITBOX_SIZE
        hitbox.Transparency = 0.5 -- debugging
        hitbox.CanCollide = false
        hitbox.Anchored = true
        hitbox.CanQuery = false
        hitbox.Massless = true
        
        hitbox.Color = Color3.fromRGB(255, 0, 0)
        hitbox.CFrame = hrp.CFrame * CFrame.new(0, 0, -ATTACK_OFFSET) 
        hitbox.Parent = workspace.Hitboxes
        
        -- cleanup
        Debris:AddItem(hitbox, 0.2)
        
        
        -- params
        local overlapParams = OverlapParams.new()
        overlapParams.FilterDescendantsInstances = {enemy} -- dont count yourself
        overlapParams.FilterType = Enum.RaycastFilterType.Exclude
        
        local partsinHitbox = workspace:GetPartsInPart(hitbox, overlapParams)
        local charactersHit = {}```
#
        -- for some reason if theyre attacking the player they can friendly fire on their own
        -- ??
        for _, part in ipairs(partsinHitbox) do
            -- oh because its counting ANY humanoid part in the hitbox as hithunm, even if its an enemy
            local character = part.Parent
            local hitHumanoid = character:FindFirstChild("Humanoid")
            
            if hitHumanoid and hitHumanoid.Health > 0 and not charactersHit[character] then
                charactersHit[character] = true -- theyre hit
                
                
                
                -- even though we spawn a forcefield onto the player when they're parrying, we can
                -- still have this here as a failsafe 
                local player = Players:GetPlayerFromCharacter(target) 
                
                
                if player then
                    if not parryingPlayer[player] then
                        hitHumanoid:TakeDamage(ATTACK_DAMAGE)
                        print("Enemy hit " .. character.Name .. " for " .. ATTACK_DAMAGE .. " damage!")
                    else
                        print("Enemy missed " .. character.Name .. "!")        
                    end
                else
                    print("I avoided hitting my teammate!")
                end
            end
        end
        
    end
end```
#

had to split it sorry

humble steppe
#

Make sure the humanoid is the players and not the enemies

olive bloom