Hello fellow developers! So today I tried making a moving NPC in my game, and I found this script for it. But, for some reason, this script won’t work. When I join the game, It just stays in place instead of going to the first part.
local NPC = game.Workspace.NPC.Leader
while true do
wait(35)
NPC.Humanoid:MoveTo(game.Workspace.Points.Point1.Position)
NPC.Humanoid.MoveToFinished:Wait()
wait(1)
NPC.Humanoid:MoveTo(game.Workspace.Points.Point2.Position)
NPC.Humanoid.MoveToFinished:Wait()
wait(8)
NPC.Humanoid:MoveTo(game.Workspace.Points.Point3.Position)
NPC.Humanoid.MoveToFinished:Wait()
wait(8)
NPC.Humanoid:MoveTo(game.Workspace.Points.Point4.Position)
NPC.Humanoid.MoveToFinished:Wait()
end
If anyone asks, I have four parts all named as it says in the script. Someone, please help me, I am stuck.
local function moveTo(humanoid, targetPoint, andThen)
local targetReached = false
local connection
connection = humanoid.MoveToFinished:Connect(function(reached)
targetReached = true
connection:Disconnect()
connection = nil
if andThen then
andThen()
end
end)
humanoid:MoveTo(targetPoint)
--spawn(function()
while not targetReached do
if not (humanoid and humanoid.Parent) then
break
end
if humanoid.WalkToPoint ~= targetPoint then
break
end
humanoid:MoveTo(targetPoint)
wait(6)
end
if connection then
connection:Disconnect()
connection = nil
end
--end)
end
local Points = workspace:WaitForChild("Points"):GetChildren()
local NPC = workspace.NPC.Leader
local Humanoid = NPC:WaitForChild("Humanoid")
table.sort(Points, function(a, b)
return tonumber(a.Name:sub(6)) < tonumber(b.Name:sub(6))
end)
while true do
for i, v in ipairs(Points) do
moveTo(Humanoid, v.Position)
end
wait(35)
end