So my turret is supposed to target the soldier however it either aims in the complete opposite direction or aims the way the soldier is facing (its supposed to track it btw to shoot at it).
My code:
local gun = script.Parent
function findTarget()
local dist = 200
local target = nil
for i,v in pairs(workspace:GetChildren()) do
local torso = v:FindFirstChild("Torso")
local human = v:FindFirstChild("Humanoid")
if v.Name == "Soldier" and torso and human then
if (gun.Position - torso.Position).magnitude < dist and human.Health > 0 then
dist = (gun.Position - torso.Position).magnitude
target = torso
end
end
end
return target
end
while wait(1) do
local torso = findTarget()
if torso then
gun.CFrame = CFrame.new(gun.Position, torso.Position)
end
end?
