Here is my code.
I have tried everything but the model never moves.
local larm = script.Parent:FindFirstChild("Left Arm")
local rarm = script.Parent:FindFirstChild("Right Arm")
function findNearestTorso(pos)
local list = game.Workspace:GetChildren()
local torso = nil
local dist = 10000
local temp = nil
local human = nil
local temp2 = nil
for x = 1, #list do
temp2 = list[x]
if (temp2:IsA("Model")) and (temp2 ~= script.Parent) then
temp = temp2:FindFirstChild("HumanoidRootPart")
human = temp2:FindFirstChildWhichIsA("Humanoid")
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
if torso ~= nil then
print("Torso found at position: ", torso.Position)
else
print("No torso found nearby.")
end
return torso
end
while true do
wait(0.1)
local target = findNearestTorso(script.Parent.HumanoidRootPart.Position)
if target ~= nil then
local humanoid = script.Parent.Humanoid
print("Current position: ", humanoid.Parent.PrimaryPart.Position)
print("Moving to target position: ", target.Position)
humanoid:MoveTo(target.Position)
humanoid.MoveToFinished:Wait() -- Wait until the movement is finished
print("After moving, new position: ", humanoid.Parent.PrimaryPart.Position)
end
end