How to detect multiple humanoids in range

So I’m trying to make a tower defense game and I’m trying to make one character be able to damage multiple enemies in range at same time. The problem is that the tower attacks only one target, and I couldn’t find any helpful tutorial about Detecting Multiple Humanoids in range and do something with them.
Does anyone know how to do this? I just got this which means it only targets the closest enemy.

You need to create an algorithm that gets the closest enemies in range and returns them as a table.

e.g

local function FindNearestTargets(radius: number?, maximumTargets: number?): {Model?}
	radius = if radius then radius else 50
	maximumTargets = if maximumTargets then maximumTargets else math.huge
	
	local targets = {}
	for i, target in mobs:GetChildren() do
		local distance = (tower.Position - target.HumanoidRootPart.Position).Magnitude
		
		if distance <= radius then
			table.insert(targets, target)
		end
	end
	
	return targets
end
1 Like

Thanks, the distance works and both npcs died at same time!