Problem about finding the Nearest object

Hello there Developers I was doing cool stuff in my studio doing some research and I got stuck about finding the Nearest Target it seems like not running and printing nil
if you have any suggestions or solutions I would be pleased to listen to everyone’s ideas :happy3:

while wait(FireRate) do
	
	local Target = nil
	local nearestTarget, nearestDistance= nil,nil
	
	for i,v in pairs(game.Workspace.Enemy:GetChildren()) do
		
		local hum = v:FindFirstChild("Humanoid")
		local torso = v:FindFirstChild("Torso")
		local HumanoidRootPart = v:FindFirstChild("HumanoidRootPart")
		local LeftArm = v:FindFirstChild("Left Arm")
		local RightArm = v:FindFirstChild("Right Arm")
		
			if (torso.Position - ShootingPart.Position).magnitude < Range and hum and torso and hum.Health > 0 or (nearestDistance and (torso.Position - ShootingPart.Position).magnitude >= nearestDistance) then
				print(torso.Parent)
				continue
		end
		print("working?") --Problem is at this line it seems like this lines not run
		nearestDistance = (torso.Position - ShootingPart.Position).magnitude 
		nearestTarget = torso
	end
	
	print(nearestTarget)
			local RayDetect = Ray.new(script.Parent.Parent.Barrel.Position, (nearestTarget.Position - script.Parent.Parent.Barrel.Position).Unit * 500)
				local hit, position = game.Workspace:FindPartOnRayWithIgnoreList(RayDetect, {ShootingPart,script.Parent.Parent.BarrelBody,script.Parent.Parent.BodyCorner,script.Parent.Parent.BodyMain,script.Parent.Parent.Sensor,script.Parent.Parent.Barrel,script.Parent.Parent.Barrel2})
			if hit == nearestTarget then
				Target = nearestTarget
	end
	
	--------------------------------------------------------------

Here are my output images

image

function FindClosestInstance(part,instances)
	local clostest,distance = _,math.huge
	for _,instance in instances do
		local diff = (instance.Position-part.Position).Magnitude
		if instance:IsA('BasePart') and diff < distance then
			clostest,distance = instance,diff
		end
	end
end
while wait(FireRate) do

	local Target = nil
	local nearestTarget, nearestDistance = nil, Range
	local ShootingPartPos = ShootingPart.Position

	for i,v in pairs(game.Workspace.Enemy:GetChildren()) do

		local hum = v:FindFirstChild("Humanoid")
		local torso = v:FindFirstChild("Torso")
		local HumanoidRootPart = v:FindFirstChild("HumanoidRootPart")
		local LeftArm = v:FindFirstChild("Left Arm")
		local RightArm = v:FindFirstChild("Right Arm")

		print(torso.Parent)
		local distance = (torso.Position - ShootingPartPos).Magnitude
		if hum and 0 < hum.Health and torso and distance < nearestDistance then
			nearestDistance = distance
			nearestTarget = torso
		end

	end

This thing is working fine but if you move some torso it will overwrite 2 nearestTarget Variable at the same time
image

		local distance = (torso.Position - ShootingPartPos).Magnitude
		if hum and 0 < hum.Health and torso and distance < nearestDistance then
			nearestDistance = distance
			nearestTarget = torso
             print(nearestTarget.Parent) --Try put print in this lines
		end

This line checks if the torso is closer than the current nearest torso. If you want to get the actual nearest torso, you check after the for loop

while wait(FireRate) do

	local Target = nil
	local nearestTarget, nearestDistance = nil, Range
	local ShootingPartPos = ShootingPart.Position

	for i,v in pairs(game.Workspace.Enemy:GetChildren()) do

		local hum = v:FindFirstChild("Humanoid")
		local torso = v:FindFirstChild("Torso")
		local HumanoidRootPart = v:FindFirstChild("HumanoidRootPart")
		local LeftArm = v:FindFirstChild("Left Arm")
		local RightArm = v:FindFirstChild("Right Arm")

		local distance = (torso.Position - ShootingPartPos).Magnitude
		if hum and 0 < hum.Health and torso and distance < nearestDistance then
			nearestDistance = distance
			nearestTarget = torso
			print(nearestTarget.Parent) -- Prints if this is closer than the current nearest torso
		end

	end

	print(nearestTarget) -- Prints the nearest torso

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