Tower Defense system working kinda weird

I was working on the tower systems for a moment, but then i got into this…

https://gyazo.com/b7ed0c71892747df68dcb237b2265a3c

Yes, the towers are on the “First” targetting mode, i have no idea why this is happening, but its really annoying…

ModuleScript code:

local replicatedStorage = game:GetService('ReplicatedStorage')
local handlers = replicatedStorage.Handlers
local gameFolder = workspace.Game
local towerFolder = gameFolder.Towers
local gEnemies = gameFolder.Enemies
local waypoints = gameFolder.Map.Waypoints
local H_Enemy = require(handlers.EnemyHandler)

local info = {
	Damage = 5;
	Range = 20;
	Firerate = 1;
	Camo = false;
	Flying = false;
	Lead = false;
	Ghost = false;
}

function info:GetValue()
	return info
end

function info:SearchTarget(tower:Model)
	local bestTarget = nil
	local bestWaypoint = nil
	local bestDistance = nil

	for _, enemies in ipairs(gEnemies:GetChildren()) do
		local distancetoEnemy = (enemies.Position - tower.PrimaryPart.Position).Magnitude
		local distanceToWaypoint = (enemies.Position - waypoints[enemies:GetAttribute('MovingTo')].Position).Magnitude

		if (enemies.Position - tower.PrimaryPart.Position).Magnitude < 15 then
			if not bestWaypoint or enemies:GetAttribute('MovingTo') >= bestWaypoint then
				bestWaypoint = enemies:GetAttribute('MovingTo')

				if not bestDistance or distanceToWaypoint < bestDistance then
					bestDistance = distanceToWaypoint
					bestTarget = enemies
				end
			end
		end
	end
	
	return bestTarget
end

function info:Attack(tower:Model, target:Model)	
	local lastChange = tick()
	local towerAnimator = tower.Humanoid.Animator
	local idleAnim = towerAnimator:LoadAnimation(tower.Idle)
	local idleAnim2 = towerAnimator:LoadAnimation(tower.Idle2)
	local fireAnim = towerAnimator:LoadAnimation(tower.Fire)
	
	if not target or H_Enemy:GetValue(target, 'Health') < 1 then 
		if not idleAnim2.IsPlaying then
			idleAnim2:Play()
			
			for _, x in pairs(towerAnimator:GetPlayingAnimationTracks()) do
				if x.Name == 'Idle' then
					x:Stop()
				end
			end
		end
		return
	end
	
	if not idleAnim.IsPlaying then
		print('playing idle 2')
		
		for _, x in pairs(towerAnimator:GetPlayingAnimationTracks()) do
			if x.Name == 'Idle2' then
				x:Stop()
			end
		end
		
		idleAnim:Play()
	end
	
	for i = 1, 3 do
		tower.PrimaryPart.CFrame = CFrame.lookAt(tower.PrimaryPart.Position, Vector3.new(target.Position.X, tower.PrimaryPart.Position.Y, target.Position.Z))
		fireAnim:Play()
		H_Enemy:Damage(target, tower:GetAttribute('Damage'))
		
		task.wait(0.2)
	end
end

return info
1 Like

Issue is at Checking DistanceToWayPoint

function info:SearchTarget(tower:Model)
	local bestTarget = nil
	local bestWaypoint = nil
	local bestDistance = nil

	for _, enemies in ipairs(gEnemies:GetChildren()) do
		local distancetoEnemy = (enemies.Position - tower.PrimaryPart.Position).Magnitude
		local distanceToWaypoint = (enemies.Position - waypoints[enemies:GetAttribute('MovingTo')].Position).Magnitude

		if (enemies.Position - tower.PrimaryPart.Position).Magnitude < 15 then
			if not bestWaypoint or enemies:GetAttribute('MovingTo') >= bestWaypoint then
				if bestWaypoint < enemies:GetAttribute('MovingTo') then
					bestDistance = nil
				end
				bestWaypoint = enemies:GetAttribute('MovingTo')

				if not bestDistance or distanceToWaypoint < bestDistance then
					bestDistance = distanceToWaypoint
					bestTarget = enemies
				end
			end
		end
	end
	
	return bestTarget
end

I tested it, but the problem still persist…