Sorry the title is really vague, I can’t word things properly half the time.
I am trying to make a tower defense game, and I want to make the attacking system to where once an enemy enters the range, the tower will keep attacking until either the enemy dies or leaves the range. I then want the tower to target the newest entered enemy. How would I do this?
local zone = require(game.ServerScriptService.Modules.Main.Zone)
local attackRange = zone.new(script.Parent)
local enemies = workspace.Placing.Enemies
local dealDmg = game.ServerStorage.Events.DealDamage
local code = script.Parent.Parent.Code.Value
local damage = script.Parent.Parent:GetAttribute("Damage")
local hitDelay = script.Parent.Parent:GetAttribute("HitDelay")
local target = nil
local nextTarget = nil
local queue = {}
dealDmg.Event:Connect(function(enemy, accessCode)
if accessCode == code then
if not target then
target = enemy
elseif not nextTarget then
nextTarget = enemy
else
table.insert(queue, enemy)
end
end
end)
Here’s my current code I have. This just receives the call the enemies make when they enter a hit zone, and it checks if the given code is the same (checking if the current tower is the right tower). I was going to do a while wait() do
loop, however I don’t think this would be resource friendly as there is going to be more than one towers, and also I was going to have it check the queue()
table. I’ve been stuck on this topic all day and any help is appreciated. Thanks