Order bug

Game code tried to execute invalid order (5). Order requires an NPC target, but specified target is not an NPC.

function item_vampir:OnSpellStart()
    local caster = self:GetCaster()
    local spawn_location = self:GetAbsOrigin()
    if self.table then
        for k,v in pairs(self.table) do
        if not v:IsNull() then
            v:Destroy()
        end 
            self.table = nil    
        end
    end      
    leech = CreateUnitByName( "npc_dota_leech", spawn_location, true, caster, caster:GetOwner(), caster:GetTeamNumber())
    leech2 = CreateUnitByName( "npc_dota_leech", spawn_location, true, caster, caster:GetOwner(), caster:GetTeamNumber())
    leech3 = CreateUnitByName( "npc_dota_leech", spawn_location, true, caster, caster:GetOwner(), caster:GetTeamNumber())
    self.table = {leech,leech2,leech3}
    leech:AddNewModifier(caster, self, "item_vampir_modifier_heal", nil)
    leech:AddNewModifier(unit, nil, "modifier_leech_on_ice", {})
    leech2:AddNewModifier(caster, self, "item_vampir_modifier_heal", nil)
    leech2:AddNewModifier(unit, nil, "modifier_leech_on_ice", {})
    leech3:AddNewModifier(caster, self, "item_vampir_modifier_heal", nil)
    leech3:AddNewModifier(unit, nil, "modifier_leech_on_ice", {})
    leech:SetRenderColor(255,0,0)
    leech:SetAngles(0,RandomInt(0, 360),0)
    leech2:SetRenderColor(255,0,0)
    leech2:SetAngles(0,RandomInt(0, 360),0)
    leech3:SetRenderColor(255,0,0)
    leech3:SetAngles(0,RandomInt(0, 360),0)
    Leech(caster)
    Leech2(caster)
    Leech3(caster)
    local hAncient = Entities:FindByName( nil, "npc_dota_hero_pudge" )
    AIThink(hAncient, caster)
    AIThink2(hAncient, caster)
    AIThink3(hAncient, caster)
end
function Leech( caster )
    local hAncient = Entities:FindByName( nil, "npc_dota_hero_pudge" )
    order.UnitIndex = leech:entindex()
    ChooseTarget(hAncient, caster)
end
function Leech2( caster )
    local hAncient = Entities:FindByName( nil, "npc_dota_hero_pudge" )
    order2.UnitIndex = leech2:entindex()
    ChooseTarget(hAncient, caster)
end
function Leech3( caster )
    local hAncient = Entities:FindByName( nil, "npc_dota_hero_pudge" )
    order3.UnitIndex = leech3:entindex()
    ChooseTarget(hAncient, caster)
end
function ChooseTarget(hAncient, caster)
    local pudge = FindUnitsInRadius( caster:GetTeamNumber(), caster:GetOrigin(), nil, 1000, DOTA_UNIT_TARGET_TEAM_ENEMY,  DOTA_UNIT_TARGET_HERO, DOTA_UNIT_TARGET_FLAG_NOT_CREEP_HERO + DOTA_UNIT_TARGET_FLAG_NOT_ANCIENTS, FIND_ANY_ORDER, false)
    if #pudge > 0 then
        hTarget = pudge[math.random(#pudge)]
        if hTarget:IsRealHero() then
        order.OrderType = DOTA_UNIT_ORDER_ATTACK_TARGET
        order2.OrderType = DOTA_UNIT_ORDER_ATTACK_TARGET
        order3.OrderType = DOTA_UNIT_ORDER_ATTACK_TARGET
        end
    else
        barrel = GameRules.barrel
        if caster:HasModifier("modifier_pudgeInTheBarrel") and not barrel:IsNull() then
            hTarget = barrel    
        else
            hTarget = caster
        end
        order.OrderType = DOTA_UNIT_ORDER_MOVE_TO_TARGET
        order2.OrderType = DOTA_UNIT_ORDER_MOVE_TO_TARGET
        order3.OrderType = DOTA_UNIT_ORDER_MOVE_TO_TARGET
    end
    if IsValidEntity(hTarget) then
    order.TargetIndex = hTarget:entindex()
    order2.TargetIndex = hTarget:entindex()
    order3.TargetIndex = hTarget:entindex()
    end
end

function AIThink(hAncient, caster)
    Timers:CreateTimer(0.5, function()
        if not hTarget:IsNull() then
            if not hTarget:IsAlive() or hTarget == caster or hTarget:HasModifier("modifier_pudge_global_eat_target") or hTarget:HasModifier("modifier_pudgeInTheBarrel") then
                ChooseTarget(hAncient, caster)
            else
                hTarget = caster
            end
            if hTarget:GetUnitName() == "npc_dota_barrel" then
                hTarget = caster
            end
        end
        if not leech:IsNull() then
        caster_speed = caster:GetMoveSpeedModifier(caster:GetBaseMoveSpeed())
        leech:SetBaseMoveSpeed(caster_speed)
        end 
        if caster:HasModifier("trigger_balvanka_modifier") then
        leech:SetBaseMoveSpeed(caster_speed + 1000)
        leech:AddNewModifier(leech, nil, "modifier_leech_speed", {})
        end
        if caster:HasModifier("modifier_pudge_global_eat_caster_scepter") or leech:HasModifier("modifier_leech_on_ice") then
        leech:AddNewModifier(leech, nil, "modifier_leech_speed", {})
        else
        leech:RemoveModifierByName("modifier_leech_speed")
        end
        if not hTarget:IsNull() then
            if ((hTarget:GetAbsOrigin() - caster:GetAbsOrigin()):Length2D()) > 1000 then
                hTarget = caster
                order.TargetIndex = hTarget:entindex()
            end
        end
        -- Got to keep issuing it in case the order drops
            if leech:IsAlive() and not leech:IsNull() and order.TargetIndex and order.UnitIndex and order.OrderType then
                ExecuteOrderFromTable(order)
            else
                return nil
            end
        return 0.5
    end)
end
function AIThink2(hAncient, caster)
    Timers:CreateTimer(0.5, function()
        if not hTarget:IsNull() then
            if not hTarget:IsAlive() or hTarget == caster or hTarget:HasModifier("modifier_pudge_global_eat_target") or hTarget:HasModifier("modifier_pudgeInTheBarrel") then
                ChooseTarget(hAncient, caster)
            else
                hTarget = caster
            end
            if hTarget:GetUnitName() == "npc_dota_barrel" then
                hTarget = caster
            end
        end
        if not leech2:IsNull() then
        caster_speed = caster:GetMoveSpeedModifier(caster:GetBaseMoveSpeed())
        leech2:SetBaseMoveSpeed(caster_speed)
        end 
        if caster:HasModifier("trigger_balvanka_modifier") then
        leech2:SetBaseMoveSpeed(caster_speed + 1000)
        leech2:AddNewModifier(leech2, nil, "modifier_leech_speed", {})
        end
        if caster:HasModifier("modifier_pudge_global_eat_caster_scepter") or leech2:HasModifier("modifier_leech_on_ice") then
        leech2:AddNewModifier(leech2, nil, "modifier_leech_speed", {})
        else
        leech2:RemoveModifierByName("modifier_leech_speed")
        end 
        if not hTarget:IsNull() then
            if ((hTarget:GetAbsOrigin() - caster:GetAbsOrigin()):Length2D()) > 1000 then
                hTarget = caster
                order2.TargetIndex = hTarget:entindex()
            end
        end
        -- Got to keep issuing it in case the order drops
            if leech2:IsAlive() and not leech2:IsNull() and order2.TargetIndex and order2.UnitIndex and order2.OrderType then
                ExecuteOrderFromTable(order2)
            else
                return nil
            end
        return 0.5
    end)
end
function AIThink3(hAncient, caster)
    Timers:CreateTimer(0.5, function()
        if not hTarget:IsNull() then
            if not hTarget:IsAlive() or hTarget == caster or hTarget:HasModifier("modifier_pudge_global_eat_target") or hTarget:HasModifier("modifier_pudgeInTheBarrel") then
                ChooseTarget(hAncient, caster)
            else
                hTarget = caster
            end
            if hTarget:GetUnitName() == "npc_dota_barrel" then
                hTarget = caster
            end
        end
        if not leech3:IsNull() then
        caster_speed = caster:GetMoveSpeedModifier(caster:GetBaseMoveSpeed())
        leech3:SetBaseMoveSpeed(caster_speed)
        end 
        if caster:HasModifier("trigger_balvanka_modifier") then
        leech3:SetBaseMoveSpeed(caster_speed + 1000)
        leech3:AddNewModifier(leech3, nil, "modifier_leech_speed", {})
        end
        if caster:HasModifier("modifier_pudge_global_eat_caster_scepter") or leech3:HasModifier("modifier_leech_on_ice") then
        leech3:AddNewModifier(leech3, nil, "modifier_leech_speed", {})
        else
        leech3:RemoveModifierByName("modifier_leech_speed")
        end 
        if not hTarget:IsNull() then
            if ((hTarget:GetAbsOrigin() - caster:GetAbsOrigin()):Length2D()) > 1000 then
                hTarget = caster
                order3.TargetIndex = hTarget:entindex()
            end
        end
        -- Got to keep issuing it in case the order drops
            if leech3:IsAlive() and not leech3:IsNull() and order3.TargetIndex and order3.UnitIndex and order3.OrderType then
                ExecuteOrderFromTable(order3)
            else
                return nil
            end
        return 0.5
    end)
end

Comments