In the game I'm creating the player's Humanoid name gets changed to "Player". When this happens the script will no longer work correctly. I tried many different things but nothing seemed to work. I'm an amateur scripter so I don't know all the ropes.
function findNearestTorso(pos)
local list = game.Workspace:children()
local torso = nil
local dist = math.huge
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") -- I tried changing Humanoid to "Player" but it didn't work either.
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
end
return torso
end
while true do
wait(0.1)
local target = findNearestTorso(script.Parent.Torso.Position)
if target ~= nil then
script.Parent.Humanoid:MoveTo(target.Position, target)
end
end

2 comments
On the line that says "human = temp2:findFirstChild("Humanoid")" I tried that but it didn't work
If it didn't work, I don't know what to tell ya. You might have to remake the script.