I am working on a small project just for fun, and to get used to the roblox engine. Im working on a small RTS game, and the script below is a unit searching for the nearest target.
function findTarget()
targetTorso = nil
local range = script.Parent.Range.Value
local list = game.Workspace:children()
for i = 1, #list do
local listTorso = list[i]:FindFirstChild("Torso")
local listTorso2 = list[i]:FindFirstChild("Team")
if listTorso ~= nil and listTorso2 ~= nil then
if (listTorso.Position - figure.Torso.Position).magnitude < range and list ~= nil and list[i].Team.Value ~= script.Parent.Team.Value then
range = (listTorso.Position - figure.Torso.Position).magnitude
targetTorso = listTorso
LOScheck(targetTorso.Position, script.Parent.Torso)
end
end
end
list = nil
end
My problem is that the event is finding up to 3 targets, even when there is 4 targets it only finds 3, but it works fine against 1 other target. What is causing the script to detect 3 targets? There are no other events that fire the function findTarget() only the one here.
there are no errors. If needed, i can provide the full script, but this is the part i'm having issues on
