Problem Solving the NPC MoveTo()

Hi Devs so Im having trouble making the NPC move to a random seat found in the dictionary but it does spawn in the workspace it just doesnt move I wrapped it in a pcall and Im not getting any errors can someone guide me on where I made a mistake…The NPC is found in ReplicatedStorage and then cloned to the workspace every 5 seconds now to make the NPC move to a random seat is the only problem Im facing I would appreciate it if someone would guide me thank you Here’s the code for review

----//Service
----//Irretation of the Seats Dictionary
local Dummys = {
game.ReplicatedStorage.NPCs.TestDummy
}

local Seats = {
	workspace.NPCSEATS.Seat,
	workspace.NPCSEATS.Seat2,
	workspace.NPCSEATS.Seat3
}
local DummySpawnPoint = workspace.NPCSEATS.NPCSpawnPoint.NPCSPAWN
---------------------------------------------------NPC SPAWNING CONTROLLER----------------------------------------------------------
local SpawnNPCs = function()
	local RandomDummy = math.random(1,#Dummys)
	local Dummy = Dummys[RandomDummy]
	local ClonedDummy = Dummy:Clone()
	ClonedDummy.Parent = game.Workspace
	Dummy.PrimaryPart.CFrame = DummySpawnPoint.CFrame
end

local SpawnTimer = 0

    while true do
	wait(5)
	SpawnTimer = SpawnTimer + 5
	if SpawnTimer >= 30 then
		SpawnTimer = 0
	end
	if SpawnTimer % 5 == 0 then
		SpawnNPCs()
		
	end
	
	
	
end
--------------------------------------------NPC MOVEMENT SECTION--------------------------------------------------------------------
local Seats = {
	workspace.NPCSEATS.Seat,
	workspace.NPCSEATS.Seat2,
	workspace.NPCSEATS.Seat3
}

----//VARIABLES
local NPCs = game.Workspace:FindFirstChild("TestDummy")
local HRP = NPCs:WaitForChild("HumanoidRootPart")
local humanoid= NPCs:FindFirstChild("Humanoid")

local closesSeat = nil
local closestSeatDistance = math.huge

----//Irriterate through the Seats Dictionay and calculates the Distance from the Model

local success, errormessage = pcall(function()
	--RandomSitSelec
	local RandomSeats = math.random(1,#Seats)
	closesSeat = Seats[RandomSeats]
	table.remove(Seats,RandomSeats)
	humanoid:MoveTo(closesSeat.Position)
	humanoid.MoveToFinished:Wait()
	humanoid:Sit(closesSeat)


end) --End of pcall

if success then
	print("NPC INIT! ")
else
	print("NPC INIT! ")
end

1 Like

If it’s a single script, then the problem is that it happens indefinitely, then everything below will not work, use something like task.spawn()

task.spawn(function()
	while true do
		wait(5)
		SpawnTimer = SpawnTimer + 5
		if SpawnTimer >= 30 then
			SpawnTimer = 0
		end
		if SpawnTimer % 5 == 0 then
			SpawnNPCs()
		end
	end
end)

Hi so I manage to make the NPC spawn and go to a random part but my problem is that it spawns only once here’s the code if you can please review it

local NPCName = "TestDummy"
local NPC = workspace:FindFirstChild(NPCName)
local NpcHumanoid = NPC.Humanoid
local NewNPC = NPC:Clone()
local NewNpcHum = NewNPC:FindFirstChild("Humanoid")


local Seats = {
	workspace.NPCSEATS.Seat,
	workspace.NPCSEATS.Seat2,
	workspace.NPCSEATS.Seat3
}


function Loop()
	NewNPC.Parent = workspace
end

local spawnTimer = 0

while true do
	wait(10)
	spawnTimer = spawnTimer + 1
	if spawnTimer <= 1 then
		spawnTimer = 0
	Loop()
	end
end


function NPCmoveToRandomSeats()
	
	local RandomSeats = math.random(1,#Seats)
	local ChosenSeats = Seats[RandomSeats]
	NPC.Humanoid:MoveTo(ChosenSeats.Position)
	NPC.MoveToFinished:Wait()
	NPC:Sit(ChosenSeats)
	
end

for _,NPC in ipairs(workspace:GetChildren())do
	if NPC.Name == NPCName then
		NPCmoveToRandomSeats()
	end
end



Since your function does not create a new NPC, but simply moves the old one to the Workspace.

Yes it does move to workspace I don’t undersand the point?

I don’t understand you, you say that there is only 1 NPC, but you clone it only 1 time, how can there be more of them?

I see where your getting confused so I want to make this NPC move to workspace and goes to random seats…so for now theres just one