local function getEnemyTarget(
targetMode: "first" | "closest" | "furthest" | "last" | "strongest" | "weakest",
towerPos: CFrame,
range: number
)
local TargetingData = enemyReplicator.getTargetingData()
local enemyTarget = nil
local potentialEnemyTranslated
local potentialEnemyDistance
local potentialEnemyHealth
for _, enemy in pairs(TargetingData) do
local hash = enemy[1]
local position: CFrame = enemy[2]
local translated = enemy[3]
local health = enemy[4]
local distance = (towerPos.Position - position.Position).Magnitude
if distance > range then
continue
end
if not enemyTarget then
potentialEnemyTranslated = translated
potentialEnemyDistance = (towerPos.Position - position.Position).Magnitude
potentialEnemyHealth = health
enemyTarget = enemy
continue
end
if targetMode == "first" then
if translated > potentialEnemyTranslated then
potentialEnemyTranslated = translated
potentialEnemyDistance = (towerPos.Position - position.Position).Magnitude
potentialEnemyHealth = health
enemyTarget = enemy
continue
end
end
if targetMode == "last" then
if translated < potentialEnemyTranslated then
potentialEnemyTranslated = translated
potentialEnemyDistance = (towerPos.Position - position.Position).Magnitude
potentialEnemyHealth = health
enemyTarget = enemy
continue
end
end
if targetMode == "furthest" then
if distance > potentialEnemyDistance then
potentialEnemyTranslated = translated
potentialEnemyDistance = (towerPos.Position - position.Position).Magnitude
potentialEnemyHealth = health
enemyTarget = enemy
continue
end
end
if targetMode == "closest" then
if distance < potentialEnemyDistance then
potentialEnemyTranslated = translated
potentialEnemyDistance = (towerPos.Position - position.Position).Magnitude
potentialEnemyHealth = health
enemyTarget = enemy
continue
end
end
if targetMode == "strongest" then
if health > potentialEnemyHealth then
potentialEnemyTranslated = translated
potentialEnemyDistance = (towerPos.Position - position.Position).Magnitude
potentialEnemyHealth = health
enemyTarget = enemy
continue
end
end
if targetMode == "weakest" then
if health < potentialEnemyHealth then
potentialEnemyTranslated = translated
potentialEnemyDistance = (towerPos.Position - position.Position).Magnitude
potentialEnemyHealth = health
enemyTarget = enemy
continue
end
end
end
return enemyTarget
end
local function getBossTarget(
targetMode: "first" | "closest" | "furthest" | "last" | "strongest" | "weakest",
towerPos: CFrame,
range
)
--> TO DO
return nil
end
local function getRaiderTarget(
targetMode: "first" | "closest" | "furthest" | "last" | "strongest" | "weakest",
towerPos: CFrame,
range: number
)
local map: Folder = workspace:WaitForChild(ReplicatedStorage.Map.Value)
local raiders: Folder = map:WaitForChild("raiders") :: Folder
local raiderTarget = nil
local potentialRaiderDistance
local potentialRaiderHealth
for _, raiderModel: Model in pairs(raiders:GetChildren() :: any) do
local cframePos = towerPos.Position
local raiderPrimary = raiderModel.PrimaryPart :: BasePart
local RaiderHumanoid: Humanoid = raiderModel:WaitForChild("Humanoid") :: Humanoid
local raiderPos = raiderPrimary.Position
local currentRaiderDistance = (cframePos - raiderPos).Magnitude
local currentRaiderHealth = RaiderHumanoid.Health
if currentRaiderDistance > range or currentRaiderHealth <= 0 then
continue
end
if not raiderTarget then
raiderTarget = raiderModel
local PrimaryPart = raiderModel.PrimaryPart :: BasePart
local Humanoid: Humanoid = raiderModel:WaitForChild("Humanoid") :: Humanoid
potentialRaiderDistance = (cframePos - PrimaryPart.Position).Magnitude
potentialRaiderHealth = Humanoid.Health
continue
end
if targetMode == "first" then
if currentRaiderDistance < potentialRaiderDistance then
potentialRaiderDistance = (cframePos - raiderPrimary.Position).Magnitude
potentialRaiderHealth = RaiderHumanoid.Health
continue
end
end
if targetMode == "last" then
if currentRaiderDistance > potentialRaiderDistance then
potentialRaiderDistance = (cframePos - raiderPrimary.Position).Magnitude
potentialRaiderHealth = RaiderHumanoid.Health
continue
end
end
if targetMode == "furthest" then
if currentRaiderDistance > potentialRaiderDistance then
potentialRaiderDistance = (cframePos - raiderPrimary.Position).Magnitude
potentialRaiderHealth = RaiderHumanoid.Health
continue
end
end
if targetMode == "closest" then
if currentRaiderDistance < potentialRaiderDistance then
potentialRaiderDistance = (cframePos - raiderPrimary.Position).Magnitude
potentialRaiderHealth = RaiderHumanoid.Health
continue
end
end
if targetMode == "strongest" then
if currentRaiderHealth > potentialRaiderHealth then
potentialRaiderDistance = (cframePos - raiderPrimary.Position).Magnitude
potentialRaiderHealth = RaiderHumanoid.Health
continue
end
end
if targetMode == "weakest" then
if currentRaiderHealth < potentialRaiderHealth then
potentialRaiderDistance = (cframePos - raiderPrimary.Position).Magnitude
potentialRaiderHealth = RaiderHumanoid.Health
continue
end
end
end
return raiderTarget
end
local function detectenemies(
targetMode: "first" | "closest" | "furthest" | "last" | "strongest" | "weakest" | "random",
range: number,
towerPos: CFrame,
priority: "enemies" | "raiders" | "none",
self: Tower
)
local map: Folder = workspace:WaitForChild(ReplicatedStorage.Map.Value)
local raiderfolder: Folder = map:WaitForChild("raiders") :: any
local attacker = self.attacker
local health = self.health
local panicHealth = (0.40 * self.maxHealth)
if attacker then
if health < panicHealth then
return attacker
end
end
local TargetingData = enemyReplicator.getTargetingData()
local RaiderTargetingData: Folder = map:WaitForChild("raiders") :: Folder
if targetMode == "random" then
local index = math.random(1, math.clamp(#TargetingData, 1, 999999))
local randomTarget = TargetingData[index]
return randomTarget
end
local enemyCount = enemyReplicator.getEnemyCount()
local raiderCount = #RaiderTargetingData:GetChildren()
local enemyTarget = getEnemyTarget(targetMode, towerPos, range)
local raiderTarget = getRaiderTarget(targetMode, towerPos, range)
local bossTarget = getBossTarget(targetMode, towerPos, range)
if priority == "enemies" then
if enemyCount > 0 then
return enemyTarget
else
return raiderTarget
end
end
if priority == "raiders" then
if raiderCount > 0 then
return raiderTarget
else
return enemyTarget
end
end
if priority == "none" then
if targetMode == "first" or targetMode == "last" then
if enemyCount > 0 then
return enemyTarget
else
return raiderTarget
end
end
if targetMode == "closest" then
if enemyTarget then
local enemyPosition: CFrame = enemyTarget[2]
local enemyDistance = (enemyPosition.Position - towerPos.Position).Magnitude
local RaiderPrimary = raiderTarget.PrimaryPart :: BasePart
local raiderPosition = RaiderPrimary.Position
local raiderDistance = (raiderPosition - towerPos.Position).Magnitude
if enemyDistance > raiderDistance then
return raiderTarget
else
return enemyTarget
end
else
return raiderTarget
end
end
if targetMode == "furthest" then
if enemyTarget then
local enemyPosition: CFrame = enemyTarget[2]
local enemyDistance = (enemyPosition.Position - towerPos.Position).Magnitude
local RaiderPrimary = raiderTarget.PrimaryPart :: BasePart
local raiderPosition = RaiderPrimary.Position
local raiderDistance = (raiderPosition - towerPos.Position).Magnitude
if enemyDistance < raiderDistance then
return raiderTarget
else
return enemyTarget
end
end
end
if targetMode == "strongest" then
if enemyTarget then
local enemyHealth = enemyTarget[4]
local raiderHumanoid: Humanoid = raiderTarget:WaitForChild("Humanoid") :: Humanoid
local raiderHealth = raiderHumanoid.Health
if enemyHealth > raiderHealth then
return enemyTarget
else
return raiderTarget
end
else
return raiderTarget
end
end
if targetMode == "weakest" then
if enemyTarget then
local enemyHealth = enemyTarget[4]
local raiderHumanoid: Humanoid = raiderTarget:WaitForChild("Humanoid") :: Humanoid
local raiderHealth = raiderHumanoid.Health
if enemyHealth < raiderHealth then
return enemyTarget
else
return raiderTarget
end
else
return raiderTarget
end
end
end
return nil :: any
end
Thanks for feedback in advance!