I’m trying to make a lock to target system. I need to know how to detect the nearest enemy. All enemies are in a folder in workspace. I have a key that you press to enable the target lock, and i know how to move the camera.
Help would be appriciated!
2 Likes
Can you give more information please.
3 Likes
information given
To detect which is the nearest enemy you need to get the children.
You also need to use Magnitude here how to do it.
local self = script.Parent
local Away = 30
while wait() do
for _,v in ipairs(workspace.Enemy:GetChildren()) do
local enm = v
local Ro = (self.CFrame.p - enm.PrimaryPart.CFrame.p).Magnitude
if Ro <= Away then
-- Script
print(enm.Name)
break
else
-- If not found
end
end
end
You can change CFrame.p to position and they both will get you what you wanted.
2 Likes