:Moveto() problem

I have a problem with my script that has :Moveto() because i wanted to try something new. I have a charcater1 and character2, I want the character 1 to move to a humanoid (or character2), it does work however, after i move the charcater2 from its spot, character 1 still doesn’t follow but rather goes to the old position of character 2. This is my script

local torsoop = script.Parent.Torso
local hum = script.Parent.Humanoid

local function findtarget()
	local distance = 10
	local target = nil
	for i, v in pairs(game.Workspace:GetChildren()) do
		print(v)
		local human = v:FindFirstChild("Humanoid")
		local tors = v:FindFirstChild("Torso")
		if human and tors and v ~= script.Parent then
			if (torsoop.Position - tors.Position).magnitude > distance then
				distance = (torsoop.Position - tors.Position).magnitude
				print(distance)
				target = tors
			end
		end
	end
	return target
end

while wait(1) do
	local torso = findtarget()
	if torso then
		hum:MoveTo(torso.Position)
	end
end
1 Like

Try this:

--//Variables
local Character = script.Parent
local torsoop = Character.Torso
local hum = Character.Humanoid

--//Functions
local function findtarget()
	local distance = 10
	local target = nil
	
	for i, child in ipairs(workspace:GetChildren()) do
		if child == Character then
			continue
		end
				
		local human = child:FindFirstChildWhichIsA("Humanoid")
		local tors = child:FindFirstChild("Torso")
		
		if human and tors then
			local newDistance = (torsoop.Position - tors.Position).Magnitude
			
			if newDistance > distance then
				distance = newDistance
				target = tors
				
				print(distance)
			end
		end
	end
	
	return target
end

--//Loops
while task.wait(0.1) do
	local torso = findtarget()
	print(torso)
	
	if torso then
		hum:MoveTo(torso.Position)
	end
end

It still doesn’t work, I have printed the position of torso but to my surprise, when i move the character, the torso’s position doesn’t change

Torso is deprecated, use humanoidroot part.

1 Like