How to make boat shoot nearest other boat

What do you want to achieve: I want to make a boat (its a union) that just shoots the nearest boat if its named a certain thing.

What is the issue?: I can’t find out how to find the nearest part, how would I get it?

So I’m making a boat that is a Union, it shoots the nearest boat if its name is Boat.
Please keep in mind, i want the boat to shoot only OTHER boats, and not the boat the script is inside of.

local part1 = -- i dont know

local part2= script.Parent

while true do

wait()

local magnitude = (part1.Position - part2.Position).magnitude

if magnitude < 50 then

--nothing here yet

wait()

end

end

You can use a simple minimum algorithm

local allBoats = --get all boats in a table
local min = 50 --also serves as maximum shooting range
local target = nil
for i,boat in next, allBoats do
    if theBoatThatsShooting ~= boat then --check if same boat
        local distance = (theBoatThatsShooting.Position-boat.Position).Magnitude --distance between two
        if distance < min then --if distance less than minimum distance so far
           min = distance --set new minimum so far
           target = boat --keep setting target for minimum so far until you reach the end 
        end
    end
end
--target variable is the boat to shoot, if no boat in range its nil

I’ll try this, thanks, It’ll help alot in my game.

Theres gonna be tons of boats, like they will be cloned. Is it possible to get every boat in a folder into the table?

folder:GetChildren() will give you a table of everything in the folder

Thanks a bundle, and i’m guessing theboatthatsshooting is just script.Parent?
(PS; this script is inside the boat)

Hey, sorry to bother but it says this.
Workspace.BoatsAndMore.inhabitnpc.Boat.Script:7: attempt to perform arithmetic (sub) on Vector3 and nil
I have no idea what this means.
heres the rest of my code

local allBoats = {workspace.BoatsAndMore:GetChildren()}
local min = 70 --also serves as maximum shooting range
local target = nil
local theBoatThatsShooting = script.Parent
for i,boat in next, allBoats do
	if theBoatThatsShooting ~= boat then --check if same boat
		local distance = (theBoatThatsShooting.Position-boat.Position).Magnitude --distance between two
		if distance < min then --if distance less than minimum distance so far
			min = distance --set new minimum so far
			target = boat
			target:Destroy()
		end
	end
end

You dont need the {} around the getchildren, it makes a table in and of itself
Also yes its the scripts boat