"scout" tower not working? tower defense project

this is a repost because nobody answered the original: "scout" Tower still thinks dead zombie is alive tower defense?

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

1 Like

Try removing the events out of the loop. You only need to run :Connect one time for each event.

I’ve also encountered problems with looping events. I had a zombie script that used ontouch in a loop and that caused the zombie to hurt me even if I was not touching it.

1 Like

it works on the first one, then the rest go past it, but when they go outside of it 5 studs orsomething some of them die.

1 Like

it might be because of the zombies are still in the table, and they are duplicated in the table:
{
[1] = Normal1,
[2] = Normal1,
[3] = Normal2,
[4] = Normal2,
[5] = Normal3,
[6] = Normal3,
[7] = Normal4,
[8] = Normal4,
[9] = Normal5,
[10] = Normal5,
[11] = Normal6,
[12] = Normal6
}

it is supposed to add only one, and it doesnt remove them

It looks like you are checking if x is in the table, and if it isn’t you add x.Parent. This will mean that if the same x does somehow enter the zone again, it would still enter x.Parent into the table since it is checking for x, not x.Parent.

I also really recommend you to put the event connections above the loop, it doesn’t hurt to try it, and you may solve some problems.

Here you are removing enemies from the enemies table when they exit the zone/get out of range. Are you also removing the enemies from the table when they die?

Also, maybe instead of removing the first enemy from the table remove x or x.Parent? Right now, the first enemy that enters the zone would be the first enemy that leaves the zone, but what if you had faster enemies? If that happened, then the last enemy that enters the zone may be the first to leave, since they are faster. So maybe change your code to this:

table.remove(enemies, x.Parent)
target = nil
1 Like

now one zombie dies, the rest just go past the scout.

1 Like

i can send you the game file to see it for yourself, well, if you want.

So after one zombies dies, what happens to the scout? Is it still shooting the zombie or doing nothing? Have you made sure you removed the zombie from the enemies table after they die?

1 Like

let me check what happens.
30 ch ars

when they first spawn, target is nil, when the first zombie enters, target gets set to Normal1, and kills him, once he is dead, target is nil, but the enemy table still has him, then target is normal1 again, but this time enemy table has normal2, then so on and so on.

Did you remove the zombie from the table when it dies? I can see the code where you set target to nil?

1 Like
--[[local parent = script.Parent

local Zone = require(game.ServerScriptService.Zone)
local zone = Zone.new(parent)
local enemies = {}
local target = nil

zone.partEntered:Connect(function(x)
	if table.find(enemies, x.Parent) == 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, table.find(enemies, x.Parent))
	target = nil
	pcall(function()
		target = enemies[1]
	end)
end)
]]--
while wait(1) do
	print("i am shooting"..tostring(target))
	table.foreach(enemies, print)
	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

It looks like you are just setting the target to nil, and not removing the target from the enemies table. Try replacing that with this:

if err then
    -- First remove the target from the table
    table.remove(enemies, target)
    -- Then set target to nil
    target = nil
end
1 Like

so zone.partExited is useless?

Based on my assumptions, you are using partExited to detect when a zombie has left the zone. The code I sent to you does not do that, it detects when the zombie has been killed. Once that happens, it will remove the zombie from the table.

1 Like

the code successfully killed 3 zombies, but there is a slight delay in the code, it takes 1 second to kill a zombie, but it took a little longer for the rest.

Did the scout stop working after that?

Can you clarify? When does the delay occur? Does it occur after the zombie is damaged?

How long for the rest? Is it very significant?

If you can, can you send some screenshots/videos? If you don’t have a screen recorder then you don’t need to send a video.

Also, have you tried moving your event connections outside above the while loop?

1 Like

i can send you the game file if you want

Sorry, I can’t open studio right now, I am on a different computer, however you can send the file and I’ll check it out later.

In the mean time, can you further describe the current problems?

1 Like

well okay, the first and second takes one second to die, the third and forth and fifth, 2 seconds, and the last one is unknown.