How to get NPC to move to a part with :MoveTo()

I want to get an npc to move to a part in a folder, which is in the workspace. There is an error with the :MoveTo() line and it says “Unable to cast Instance to Vector3”.

This is the script:

local ServerStorage = game:GetService("ServerStorage")
local NPCFolder = ServerStorage.NPCs
local NPCEnd = game.Workspace.Track.NPCEnd

while true do
	local randomnumber = math.random(1,2)
	if randomnumber == 2 then
		local NPC = NPCFolder.Bacon:Clone()
		local Humanoid = NPC.Humanoid
		NPC.Parent = game.Workspace
		task.wait(0.05)
		NPC.HumanoidRootPart.CFrame = game.Workspace.Track.NPCSpawn.CFrame
		Humanoid:MoveTo(NPCEnd)
	else
		randomnumber = math.random(1,4)
		if randomnumber == 4 then
			local NPC = NPCFolder.Noob:Clone()
			local Humanoid = NPC.Humanoid
			NPC.Parent = game.Workspace
			task.wait(0.05)
			NPC.HumanoidRootPart.CFrame = game.Workspace.Track.NPCSpawn.CFrame
			Humanoid:MoveTo(NPCEnd)
		else
			local NPC = NPCFolder.Bacon:Clone()
			local Humanoid = NPC.Humanoid
			NPC.Parent = game.Workspace
			task.wait(0.05)
			NPC.HumanoidRootPart.CFrame = game.Workspace.Track.NPCSpawn.CFrame
			Humanoid:MoveTo(NPCEnd)
		end
	end
	task.wait(5)
end

Thank you for reading and hopefully replying!

2 Likes

Just add a .Position at the end.

local ServerStorage = game:GetService("ServerStorage")
local NPCFolder = ServerStorage.NPCs
local NPCEnd = game.Workspace.Track.NPCEnd

while true do
	local randomnumber = math.random(1,2)
	if randomnumber == 2 then
		local NPC = NPCFolder.Bacon:Clone()
		local Humanoid = NPC.Humanoid
		NPC.Parent = game.Workspace
		task.wait(0.05)
		NPC.HumanoidRootPart.CFrame = game.Workspace.Track.NPCSpawn.CFrame
		Humanoid:MoveTo(NPCEnd.Position)
	else
		randomnumber = math.random(1,4)
		if randomnumber == 4 then
			local NPC = NPCFolder.Noob:Clone()
			local Humanoid = NPC.Humanoid
			NPC.Parent = game.Workspace
			task.wait(0.05)
			NPC.HumanoidRootPart.CFrame = game.Workspace.Track.NPCSpawn.CFrame
			Humanoid:MoveTo(NPCEnd.Position)
		else
			local NPC = NPCFolder.Bacon:Clone()
			local Humanoid = NPC.Humanoid
			NPC.Parent = game.Workspace
			task.wait(0.05)
			NPC.HumanoidRootPart.CFrame = game.Workspace.Track.NPCSpawn.CFrame
			Humanoid:MoveTo(NPCEnd.Position)
		end
	end
	task.wait(5)
end
1 Like

Thanks! This definitely solves the question but can I just ask why the npc stops walking about 35 studs from the starting point? It still has around 55 studs to go and it just stops before it can make it. The npcs seem to make it further with an increased walkspeed, but to make it the walkspeed would beed to be around 50 and thats much more than I want it.

I see some issues with your code. Here’s a fix:

local ServerStorage = game:GetService("ServerStorage")
local NPCFolder = ServerStorage.NPCs
local NPCEnd = game.Workspace.Track.NPCEnd

while true do
	local randomnumber = math.random(1,8)
	if randomnumber == 8 then
		local NPC = NPCFolder.Noob:Clone()
		NPC.Parent = game.Workspace
		local Humanoid = NPC.Humanoid
		NPC.HumanoidRootPart.CFrame = game.Workspace.Track.NPCSpawn.CFrame
		Humanoid:MoveTo(NPCEnd.Position)
	else
		local NPC = NPCFolder.Bacon:Clone()
		NPC.Parent = game.Workspace
		local Humanoid = NPC.Humanoid
		NPC.HumanoidRootPart.CFrame = game.Workspace.Track.NPCSpawn.CFrame
		Humanoid:MoveTo(NPCEnd.Position)
	end
	task.wait(5)
end

This helps but its not really what i want anymore, I need it to do 1/2 chance, if that works then spawn that npc, then 1/4 and so on. is there a good way to do this with my script? Also i have updated the script since post.

local ServerStorage = game:GetService("ServerStorage")
local NPCFolder = ServerStorage.NPCs
local NPCEnd = game.Workspace.Track.NPCEnd

while true do
	local randomnumber = math.random(1,2)
	if randomnumber == 2 then
		local NPC = NPCFolder.Bacon:Clone()
		local Humanoid = NPC.Humanoid
		NPC.Parent = game.Workspace
		task.wait(0.05)
		Humanoid:MoveTo(NPCEnd.Position)
	else
		randomnumber = math.random(1,4)
		if randomnumber == 4 then
			local NPC = NPCFolder.Noob:Clone()
			local Humanoid = NPC.Humanoid
			NPC.Parent = game.Workspace
			task.wait(0.05)
			NPC.HumanoidRootPart.CFrame = game.Workspace.Track.NPCSpawn.CFrame
			Humanoid:MoveTo(NPCEnd.Position)
		else
			randomnumber = math.random(1,8)
			if randomnumber == 8 then
				local NPC = NPCFolder.Business:Clone()
				local Humanoid = NPC.Humanoid
				NPC.Parent = game.Workspace
				task.wait(0.05)
				NPC.HumanoidRootPart.CFrame = game.Workspace.Track.NPCSpawn.CFrame
				Humanoid:MoveTo(NPCEnd.Position)
			end
		end
	end
	task.wait(1)
end
local ServerStorage = game:GetService("ServerStorage")
local NPCFolder = ServerStorage.NPCs
local NPCEnd = game.Workspace.Track.NPCEnd

local NPCRarity = {[2] = "Bacon",[4] = "Noob",[8] = "Buisness"} -- used to add rarity.

local function GetNPC()
	local currentnpc = nil
	repeat
		for rarity,npc in pairs(NPCRarity) do
			if math.random(1,rarity) == 1 then
				currentnpc = npc
			end
		end
	until currentnpc ~= nil
	return currentnpc
end

while true do
	local NPCType = GetNPC()
	local NPC = NPCFolder[NPCType]:Clone()
	NPC.Parent = game.Workspace
	NPC.HumanoidRootPart.CFrame = game.Workspace.Track.NPCSpawn.CFrame
	NPC.Humanoid:MoveTo(NPCEnd.Position)
	task.wait(1)
end

Alright, I fixed the rarity thing for you ig.

this did the exact same thing as before… the npcs still stop way before the end…

Ah, sorry for not replying. Does the issue occur with only these npcs or all? Your code logic seems fine enough.