Hey!
I’m trying to make a TD game and have no idea how to make targeting. I don’t want to just be spoofed the code, I just want ideas and how I could do this.
Basically the current code just goes for the closest zombie, not the first. How would I make it target the first, and it’s still in range?
Thanks!
Code:
local TowerModule = {}
local ServerScriptService = game:GetService("ServerScriptService")
local FunctionsMainModule = require(ServerScriptService.FunctionsMain)
function TowerModule.SingleAttack(tower : Instance)
coroutine.wrap(function()
local requestMap = ServerScriptService.BindableServer.GetMap:Invoke()
local mainPart = tower.MainPart
local Enimes = requestMap.RunningZombies
local SetDistance = tower:GetAttribute("TargetDistance") or tower:SetAttribute("TargetDistance", 999999999)
for index, zombie in ipairs(Enimes:GetChildren()) do
local HRP = zombie.HumanoidRootPart
local Magnitude = (HRP.Position - tower.MainPart.Position).Magnitude
if (Magnitude < SetDistance) then
tower.Target.Value = zombie
end
end
print(tower.Target.Value)
local TowerLevel = tower.Levels:FindFirstChild(tower.Level.Value)
if tower.Target.Value ~= nil then
if tower.Target.Value:FindFirstChildOfClass("Humanoid") then
tower.Target.Value.Humanoid:TakeDamage(TowerLevel.Damage.Value)
end
end
end)()
end
return TowerModule