Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

problem with moveto?

pos = game.Workspace.moving.Position

while true do
wait()
	game.Workspace.Model.Humanoid:MoveTo(Vector3.new(pos))
end

the script above should make a humanoid move to a part. in the output, it says moving is not a valid member of workspace. and it isnt. because when the player selects a tool, the part called moving gets created. but the humanoid does not move. and i tried putting everything inside of the while true do, but it didnt help.

player = game.Players.LocalPlayer
mouse = player:GetMouse()
tool = script.Parent


local function onEquip()
brick = Instance.new("Part",game.Workspace)
brick.BrickColor = BrickColor.new(1020)
brick.Name = "moving"
brick.Size = Vector3.new( .01, 4, 4)
brick.Shape = "Cylinder"
brick.Anchored = true
mouse.TargetFilter = brick
mouse.Move:connect(function()
if mouse then
brick.CFrame = CFrame.new(mouse.Hit.X, mouse.Hit.Y, mouse.Hit.Z)
brick.Rotation = Vector3.new(0,0,-90)
end
end)
end


tool.Equipped:connect(onEquip)

tool.Unequipped:connect(function()
brick:Destroy()
end)

this is the part script, it makes the part go wherever the mouse goes.

2 answers

✓ Accepted answer
test = game.Workspace:WaitForChild('Moving')

	wait(.1)
local Human = script.Parent.NPC -- Change NPC to what the Humanoid Is named
		while wait(.1) do
	if test == nil then
		Human:MoveTo(game.Workspace.Baseplate) -- moves humanoid if part is nil
	if test ~= nil then
		Human:MoveTo(test.Position) -- moves humanoid to part  if it's not nil
	wait(.1)
		end
	end
end

Just use a :WaitForChild method. It waits for the given child to fully load.

pos = game.Workspace:WaitForChild("moving").Position

while true do
wait()
    game.Workspace.Model.Humanoid:MoveTo(Vector3.new(pos))
end

Edited

Since loops are prone to causing lag, without doubt, use a Changed event to fire whenever moving is in motion.

pos = game.Workspace:WaitForChild("moving").Position

workspace:WaitForChild("moving").Changed:connect(function()
    game.Workspace.Model.Humanoid:MoveTo(Vector3.new(pos))
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.