in the last post i used some advice to make a tower for my tower defense game that will shoot 0.5 speed, 2 damage zombies have 4 health
but in the output, when the zombie is dead, my code thinks it is still alive, and tries to shoot at it for a while, so it lets the zombies walk without dying, then it just selects another random zombie, but it is supposed to choose the second zombie, and after that it is just too late.
code:
local parent = script.Parent
local Zone = require(game.ServerScriptService.Zone)
local zone = Zone.new(parent)
local enemies = {}
local target = nil
while wait(0.5) do
zone.partEntered:Connect(function(x)
if table.find(enemies, x) == nil then
table.insert(enemies, x.Parent)
end
if x.Parent:FindFirstChild("Config") then
if target == nil then
target = enemies[1]
end
end
end)
zone.partExited:Connect(function(x)
table.remove(enemies, 1)
target = nil
pcall(function()
target = enemies[1]
end)
end)
print(target, enemies)
if target ~= nil then
local success, err = pcall(function()
target.Config.Health.Value -= 2
end)
if err then
target = nil
end
else
pcall(function()
if zone:getParts()[1].Parent:FindFirstChild("Config") then
target = enemies[1]
end
end)
end
end
btw i used zone region3