Need Help SAM(Surface to air missile)

Im trying to find the closet part in range of a magnitude of 1k studs
thats is an enemy

dont know how to get the enemy

could someone help it would be much appreciated

So i think i found a fix of using atributes

could you tell me more about it

local Launcher = script.Parent
local Main = Launcher.MainRotator

local target
local Range = 100
local function FindTarget()
for i,v in pairs(workspace:GetDescendants()) do
if v:IsA(“BasePart”)and v.Parent ~=Launcher and v.Position.Magnitude <= Range then
if v:GetAttribute(“AirPlane”) and v:GetAttribute(“Team”) ~= Launcher:GetAttribute(“Team”) then
target = v
print(v)
return target
end
end
end
end

local function

while true do
task.wait(.4)
FindTarget()
end

eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee you wanted to know?

This not good for server performance because what you doing is loop and get all childrens in entire workspace every 0.4s.

For better server performance you can use CollectionService and only loop through it

local CollectionService = game:GetService("CollectionService")

local Launcher = script.Parent
local Main = Launcher.MainRotator

local target
local Range = 100

local function FindTarget()
   for i, v in CollectionService:GetTagged("AirPlane") do
      if v:GetAttribute("Team") ~= Launcher:GetAttribute(“Team”) and (Launcher.Position - v.Position).Magnitude <= Range then
         target = v
         return v
      end
   end
end

while task.wait(0.4) do
   FindTarget()
end

You need add AirPlane Tag to planes

1 Like

Ty for trying to help me but for now i might still want to use attributes for some extra stuff