You can write your topic however you want, but you need to answer these questions:
What do you want to achieve? Keep it simple and clear!
I don’t want any npc to move with delays
What is the issue? Include screenshots / videos if possible!
the second or the third time my script spawned an NPC it’ll have this problem while the previous ones doesn’t seem to have any problem
What solutions have you tried so far? Did you look for solutions on the Creator Hub?
someone told me to set the Server as HumanoidRootPart’s network owner , and it doesn’t really work as expected, I didnt even add a single second to wait after MoveToFinished event
local canspawn = ss:WaitForChild("CanSpawnNPC1")
local function spawnNPC()
local npc = game.ReplicatedStorage:WaitForChild("NPCs"):FindFirstChild("Customer"):Clone()
local root = npc:WaitForChild("HumanoidRootPart")
local humanoid = npc:WaitForChild("Humanoid")
npc.Parent = workspace
root.CFrame = workspace:FindFirstChild("NPCSpawnCFrame").CFrame
root:SetNetworkOwner(nil)
npc.CustomizeLook.Enabled = true
npc:SetAttribute("NPCNumber", 1)
local path = ps:CreatePath()
path:ComputeAsync(root.Position, goal.Position)
local waypoints = path:GetWaypoints()
for i, waypoint in pairs(waypoints) do
humanoid:MoveTo(waypoint.Position)
humanoid.MoveToFinished:Wait()
end
makeOrder(npc)
end
while true do
task.wait(math.random(5, 10))
if canspawn.Value then
canspawn.Value = false
task.spawn(spawnNPC)
end
end
The npc moves like that because of the humanoid.MoveToFinished:Wait(), try this one instead:
local ps = game:GetService("PathfindingService")
local ss = game:GetService("ServerStorage")
local canSpawn = ss:WaitForChild("CanSpawnNPC1")
local function spawnNPC()
local npc = game.ReplicatedStorage:WaitForChild("NPCs"):WaitForChild("Customer"):Clone()
local root = npc:WaitForChild("HumanoidRootPart")
local humanoid = npc:WaitForChild("Humanoid")
local spawnCF = workspace:WaitForChild("NPCSpawnCFrame")
local goal = workspace:WaitForChild("NPCGoal")
npc.Parent = workspace
root.CFrame = spawnCF.CFrame
root:SetNetworkOwner(nil)
npc.CustomizeLook.Enabled = true
npc:SetAttribute("NPCNumber", 1)
local path = ps:CreatePath({
AgentHeight = 6,
AgentRadius = 3,
AgentCanJump = true
})
path:ComputeAsync(root.Position, goal.Position)
local waypoints = path:GetWaypoints()
local index = 1
local function goNext()
if index > #waypoints then
makeOrder(npc)
return
end
local wp = waypoints[index]
if wp.Action == Enum.PathWaypointAction.Jump then
humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
end
humanoid:MoveTo(wp.Position)
index += 1
end
humanoid.MoveToFinished:Connect(function(reached)
if reached then
goNext()
else
path:ComputeAsync(root.Position, goal.Position)
waypoints = path:GetWaypoints()
index = 1
goNext()
end
end)
goNext()
end
while true do
task.wait(math.random(5, 10))
if canSpawn.Value then
canSpawn.Value = false
task.spawn(spawnNPC)
end
end
Uh my tip is setting the networkownership to the target om the server and then using a remotevent and handle the rest on the client. This will then automatically replicate the movement
You could raycast from the NPC to the target and check if it hits, if it does it means there is no obstruction and you can just use humanoid:MoveTo() directly, otherwise you use the pathfinding nodes.
if you find replicatiom difficult, you can do a little hacky trick by checking the distance from the NPC to the waypoint and if that number is smaller than 0.5 or something it just moves to the next waypoint so it doesnt stop at every waypoint.
I can give some examples if you like, just let me know
The delay might be coming from humanoid.MoveToFinished:Wait() waiting until NPC is done and the loop blocking some main thread stuff, every 5 to 10 seconds.
Comment humanoid.MoveToFinished:Wait() and run the while loop in a separate serial thread:
local canspawn = ss:WaitForChild("CanSpawnNPC1")
local function spawnNPC()
local npc = game.ReplicatedStorage:WaitForChild("NPCs"):FindFirstChild("Customer"):Clone()
local root = npc:WaitForChild("HumanoidRootPart")
local humanoid = npc:WaitForChild("Humanoid")
npc.Parent = workspace
root.CFrame = workspace:FindFirstChild("NPCSpawnCFrame").CFrame
root:SetNetworkOwner(nil)
npc.CustomizeLook.Enabled = true
npc:SetAttribute("NPCNumber", 1)
local path = ps:CreatePath()
path:ComputeAsync(root.Position, goal.Position)
local waypoints = path:GetWaypoints()
for i, waypoint in pairs(waypoints) do
humanoid:MoveTo(waypoint.Position)
--humanoid.MoveToFinished:Wait()
end
makeOrder(npc)
end
task.spawn(function()
while true do
task.wait(math.random(5, 10))
if canspawn.Value then
canspawn.Value = false
task.spawn(spawnNPC)
end
end
end)
You can try uncommenting humanoid.MoveToFinished:Wait() afterwards if you want this
That is true, because it will skip all the waypoints instanly (because it doesnt wait anymore for it to move to the next one) so it just straightup goes to the last waypoint.
Hello, everyone! I used to have this issue, what I did to fix it was using a pathfinding library like this one: NoobPath by @grewsxb4. The main problem however was that I was calculating the next path so fast and so much that made the npc unable to walk to the waypoints fast enough making the npc take small steps.
So the final conclusion is that this error is probably caused by the speed of the script, it calculating paths faster then the npc can move, making the npc walk strange like this.
i’m actually confused how to replicate this on client side, however there is an obvious obstruction for the npc as i built a wall recently, for a temporary solution i increased the waypoint spacing