#Range isnt working?

1 messages · Page 1 of 1 (latest)

arctic crag
#

So I pass in RangeSize which is the cylinder part Y Size and somehow he still targets the enemy who also is more far away than the range cylinder itself?

#

Btw YOu just have to focus on the first target

#

because I am using the "First" Target Type only for now

arctic crag
#

You only need it to here: ```lua
function Towers.FindTarget(Tower, TargetType, UpgradeNumber, RangeSize)
local targetedEnemy = nil
local cooldown = nil
local damage = nil

local bestWaypoint = nil

if UpgradeNumber then
    cooldown = TowerSettings[Tower.Name]["Upgrades"][UpgradeNumber].AttackCooldown
    damage = TowerSettings[Tower.Name]["Upgrades"][UpgradeNumber].Damage
else
    damage = TowerSettings[Tower.Name].Damage
    cooldown = TowerSettings[Tower.Name].AttackCooldown
end


if TargetType == "First" then
    for i, enemy in ipairs(enemiesFolderWorkspace:GetChildren()) do
        local enemyWaypoint = enemy:FindFirstChild("Waypoint")
        if enemyWaypoint then
            if bestWaypoint == nil or tonumber(bestWaypoint.Name) <= tonumber(enemyWaypoint.Value.Name) then
                local enemyToTower = (Tower.PrimaryPart.Position - enemy.PrimaryPart.Position).Magnitude
                if bestWaypoint then
                    local waypointToNewEnemy = (enemyWaypoint.Value.Position - enemy.PrimaryPart.Position).Magnitude
                    local bestWaypointToNewEnemy = (bestWaypoint.Position - targetedEnemy.PrimaryPart.Position).Magnitude
                    
                    if waypointToNewEnemy <= bestWaypointToNewEnemy and enemyToTower < RangeSize then
                        targetedEnemy = enemy
                        bestWaypoint = enemyWaypoint.Value
                    end
                else
                    if enemyToTower < RangeSize then
                        targetedEnemy = enemy
                        bestWaypoint = enemyWaypoint.Value
                    end
                end
            end
        end
    end
south storm
south storm
#
        for i, enemy in ipairs(enemiesFolderWorkspace:GetChildren()) do
            local enemyWaypoint = enemy:FindFirstChild("Waypoint")
            if enemyWaypoint then
                if bestWaypoint == nil or tonumber(bestWaypoint.Name) <= tonumber(enemyWaypoint.Value.Name) then
                    local enemyToTower = (Tower.PrimaryPart.Position - enemy.PrimaryPart.Position).Magnitude
                    if bestWaypoint then
                        local waypointToNewEnemy = (enemyWaypoint.Value.Position - enemy.PrimaryPart.Position).Magnitude
                        local bestWaypointToNewEnemy = (bestWaypoint.Position - targetedEnemy.PrimaryPart.Position).Magnitude
                        
                        if waypointToNewEnemy <= bestWaypointToNewEnemy and enemyToTower < RangeSize then
                            targetedEnemy = enemy
                            bestWaypoint = enemyWaypoint.Value
                        end
                    else
                        if enemyToTower < RangeSize then
                            targetedEnemy = enemy
                            bestWaypoint = enemyWaypoint.Value
                        end
                    end
                end
            end
        end``` this whole thing is just a mess
#

maybe reduce your nesting and simplify your logic, ya?

#

you're trying to do a distance check, radius check, and waypoint check all at once and you seem to have lost yourself in the sauce along the way

#

stop trying to do everything all at once

#

coz really it seems like this attempt to do a 'best waypoint' is messing up the rest of your logic