Tower defense targeting

ive made a post like this before, but it didnt give me any solutions, here i am again.

so im making a tower defense game but the towers attack multiple enemies at time, even killing multiple at a time, which is what i dont want, but everything i tried, it just didnt work.
here is my code, i hope someone can fix it

function Look()
	for i,v in pairs(enemies:GetChildren()) do
		if v then
			if v:IsA("Model") then
				if v.PrimaryPart then
					if v and (v.PrimaryPart.Position - script.Parent.PrimaryPart.Position).Magnitude <= range/1.5 and v.Dead.Value == false then
						Attack(v)

				else
					--enemy is dead so no code should be run
			end
				end

		end
	end
	end
end

ive tried using tables, it still didnt work.

1 Like

maybe you should see if the tower is already attacking an enemy
from

function Look()
	for i,v in pairs(enemies:GetChildren()) do
		if v then
			if v:IsA("Model") then
				if v.PrimaryPart then
					if v and (v.PrimaryPart.Position - script.Parent.PrimaryPart.Position).Magnitude <= range/1.5 and v.Dead.Value == false then
						Attack(v)

				else
					--enemy is dead so no code should be run
			end
				end

		end
	end
	end
end

to

isattacking = false
function Look()
	for i,v in pairs(enemies:GetChildren()) do
		if v then
			if v:IsA("Model") then
				if v.PrimaryPart then
					if v and (v.PrimaryPart.Position - script.Parent.PrimaryPart.Position).Magnitude <= range/1.5 and v.Dead.Value == false and isattacking == false then
isattacking = true
						Attack(v)

				else
isattacking = false
					--enemy is dead so no code should be run
			end
				end

		end
	end
	end
end

thank you, i was really confused on how to do this