Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to find one target out of all children selected?

Line 43

Me again. Again, it's in a for in pairs... I want to get one target, I've tried FindFirstChild(OfClass) but it doesn't take it. How do I find a target?

local Tower = script.Parent
local towerHolder = Tower.Parent
local towerMain = towerHolder.towerMain
local shotAmmo = towerHolder.shotAmmo
local towerHealth = towerHolder.towerHealth
local damage = towerHolder.towerDamage
local Time = towerHolder.towerTimeShoot
local Enemies = game.Workspace.Bricks.Attackers

function Fire(attack)
	local ammo = Instance.new("Part",shotAmmo)
	ammo.Size = Vector3.new(.63,.63,.63)
	ammo.BrickColor = BrickColor.new("Black")
	ammo.CFrame = Tower.CFrame + Vector3.new(0,7.3,0)
	ammo.CanCollide = false
	ammo.Name = "Ammo"
	ammo.TopSurface = "Smooth"
	ammo.BottomSurface = "Smooth"
	
	for i,v in pairs(Tower.AmmoParameters:GetChildren()) do
		v.Parent.Damage.Value = damage.Value
		v:Clone().Parent = ammo
		
		--ammo.Follow.Disabled = true
	end
	
	ammo.Body.Position = attack.Position
	game.Debris:AddItem(ammo,0.5)	
end

while wait(1/Time.Value) do
	if towerHealth.Value <= 0 then --Tower health has depleted, game over
		break
	end
	
	local towerMag = towerHolder.towerMagnitude
	
	for i,v in pairs(Enemies:GetChildren()) do
		if v then -- if Enemies found then
			local target
			while wait() do
				repeat wait()
					target = v --Add something else to find only ONE target, not all children.
				until target ~= nil and target.Alive.Value == true
					local magnitude = (target.Position - towerMain.Position).magnitude
					if magnitude <= towerMag.Value then
						Fire(target)
					end
				break
			end
		end
	end
end

print("Game Over")

1 answer

✓ Accepted answer

You're already finding one target out of the table. Just not the one you want it to be.

Your code needs some polishing; you have the right idea with magnitude though. It should find the nearest target first by comparing distances with magnitude then return that target so you can do your firing code.

The for loop was only returning the last enemy in the table who exists and was alive. I've put some variables in the code to give you some ideas so you only get the nearest target.

local enemies = {"Bob","Tom","Jerry","Marshal"}

local function FindNearestTarget(targetTable)
	local target = nil 
	local MAX_SEARCH_DIST = 500 -- How many studs away do you want to search for
	local currentDist = 0 -- Compare currentDist with lastDist to see who is closest
	local lastDist = 0 -- The target who's distance was closest 
	
	-- Do your "FindNearestTarget" code here ==========
	for _,enemy in pairs(targetTable) do
        if enemy then -- if Enemy exists 
            --while wait() do -- Don't need this while loop
                repeat --wait() -- Don't need this repeat loop
                    target = enemy --Add something else to find only ONE target, not all children.
                until target ~= nil --and target.Alive.Value == true
                    --local magnitude = (target.Position - towerMain.Position).magnitude
                    --if magnitude <= towerMag.Value then
                        --Fire(target)
                    --end
                --break
            --end
        end
	end		
	-- This for loop will always return the last person in the list
	-- In your case, the last target who exists and is alive
	--====================================================
	
	return target
end

local target = FindNearestTarget(enemies)

while target and wait(1) do
	print("Found a target and his name is: " .. tostring(target))	
	-- Always prints "Found a target and his name is: Marshal"
	if target then
		-- Do firing code outside of your for loop
	end
	target = FindNearestTarget(enemies)
end

1 comment

1

Although my "enemies" all have the same name. I realized and see many other techniques I can use thanks to you. Appreciate the help immensely!

This discussion is preserved as it was. New answers, comments, and votes are closed.