This script is suppose to check if the human that is closest to the AI is friendly (same team) or an enemy (other team). It seems to have an error that I cannot resolve and I need your help. I want the AI to go after Enemies that are on the 'Red' Team and not chase humanoids who are on the Same team.
------------------------------------------------ My Script
local larm = script.Parent:FindFirstChild("Left Arm")
local rarm = script.Parent:FindFirstChild("Right Arm")
function findNearestTorso(pos)
local list = game.Workspace:children()
local torso = nil
local dist = 50
local temp = nil
local human = nil
local temp2 = nil
for x = 1, #list do
temp2 = list[x]
if (temp2.className == "Model") and (temp2 ~= script.Parent) then
temp = temp2:findFirstChild("Torso")
human=temp2:FindFirstChild('Humanoid') or temp2:FindFirstChild('RedTeam') or nil
if GetPlayerFromCharacter(temp2.Parent).TeamColor == game.Teams.Reds.TeamColor then -----THIS PART IS BROKEN!!!!
if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
if (temp.Position - pos).magnitude < dist then
torso = temp
dist = (temp.Position - pos).magnitude
end
end
end
return torso
else
findNearestTorso(script.Parent.Torso.Position)
end
end
end
while true do
wait(0.1)
local target = findNearestTorso(script.Parent.Torso.Position)
if target ~= nil then
script.Parent.RedTeam:MoveTo(target.Position, target)
end
end
-------------------------------------
Output:
Workspace.RedMinion.FollowScript:16: attempt to call global 'GetPlayerFromCharacter' (a nil value)
-------------------------------------
Can you fix my script? |