Help with Tower Defense “First” and "Last" Targeting

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    Getting the first/last target on the path.

  2. What is the issue? Include screenshots / videos if possible!
    Tower keep shooting random target when enemy turns a corner
    Screenshot 2023-11-07 181656

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I tried using the waypoint and check the distance of that first target but sadly it didn’t go as planed and yes. Most people on Developer Hub haven’t had a solution on other post.
    After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

for i, mob in ipairs(workspace.Mobs:GetChildren()) do
		local distanceToMob = (mob.HumanoidRootPart.Position - newTower.HumanoidRootPart.Position).Magnitude
		local distanceToWaypoint = (mob.HumanoidRootPart.Position - map.Waypoint[mob.MovingTo.Value].Position).Magnitude
		
		if distanceToMob <= range then
			if mode == "Near"  then
				range = distanceToMob
				bestTarget = mob
			elseif  mode == "First" then
				if not bestWaypoint or mob.MovingTo.Value > bestWaypoint then
					bestWaypoint = mob.MovingTo.Value

					if not bestDistance or distanceToWaypoint < bestDistance  then
						bestDistance = distanceToWaypoint
						bestTarget = mob
					end
				end
			elseif mode == "Last" then
				if not bestWaypoint or mob.MovingTo.Value <= bestWaypoint then
					bestWaypoint = mob.MovingTo.Value

					if not bestDistance or distanceToWaypoint > bestDistance  then
						bestDistance = distanceToWaypoint
						bestTarget = mob
					end
				end
			elseif mode == "Strong" then
				if not bestHealth or mob.Humanoid.Health > bestHealth then
					bestHealth = mob.Humanoid.Health
					bestTarget = mob
				end
			elseif mode == "Weak" then
				if not bestHealth or mob.Humanoid.Health < bestHealth then
					bestHealth = mob.Humanoid.Health
					bestTarget = mob
				end
			end
		end
		
		print(bestDistance, bestWaypoint)
	end

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

1 Like

As far as I remember the YouTuber GnomeCode has a series for creating a tower defence game and in on of the episodes he make a system where players can choose if the NPCs target the first, last, strongest and weakest.

Found the video:

Video

Please, just keep watching. GnomeCode made an issue on the Last and First targetting. Basically you have to set bestDistance to nil, he of course quickly fixed it

elseif  mode == "First" then
	if not bestWaypoint or mob.MovingTo.Value > bestWaypoint then
		bestWaypoint = mob.MovingTo.Value
		if bestWaypoint then
			bestDistance = nil
		end

		if not bestDistance or distanceToWaypoint < bestDistance  then
			bestDistance = distanceToWaypoint
			bestTarget = mob
		end
	end
elseif mode == "Last" then
	if not bestWaypoint or mob.MovingTo.Value <= bestWaypoint then
		bestWaypoint = mob.MovingTo.Value
		if bestWaypoint then
			bestDistance = nil
		end

		if not bestDistance or distanceToWaypoint > bestDistance  then
			bestDistance = distanceToWaypoint
			bestTarget = mob
		end
	end
1 Like

He copied the whole code, so he knows from whom the tutorial is.

1 Like

I already forgot most code function. I know by read them by the function name. The only issue is the targeting. I did tried a bit stuff to make the targeting more accurately since I only just start renewing all code after 5 yr of ignoring it

Alr so I found the issue. Basically the code above just makes the value nil. Then the code will just trigger again causing the “Best” into nil again.
Here’s prove:

Since when checking for the first enemy. It will find a random one at the closet because the bestWayPoint is literally nil :skull:

I suggest making a new code that doesn’t make the vale Nil again or just use Values

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.