Weird walking issue with Humanoid

Heya Dev Forum,
Currently reworking my NPC waypoint system to make it “Meddle-Proof”. Basically making it very difficult to permanently veer the NPC off course. However, I am facing an issue which has boggled me for a couple of hours now.

Essentially, the waypoint system works great but only if I’m a few studs from the NPC. If I’m at a distance from the NPC, it just stays there and triggers my in-built retry system and eventually tries to teleport itself to the start point. I’m profoundly confused about this so I’m posting this problem here.

Script:

if not game:IsLoaded() then
	game.Loaded:Wait()
end
--local npc = game.Workspace.NPCs.One.NonServingNPCS.WalkingNPCS.Walk1 -- Change this to where NPC is in workspace(explorer)
local npc = game.Workspace.NonServingNPCS.WalkingNPCS.Walk1
local humanoid = npc.NPC
local character = npc
local wayPoints = game.Workspace.NonServingNPCS.WalkingNPCS.Walking1
local walkAnim = humanoid:LoadAnimation(character.Animation)
--local count = 1
local start = wayPoints.Start
local hasReachedWaypoint = script.HasReachedWaypoint
local retryCount = script.RetryCount
local totalWaypoints = script.TotalWaypoints
local waypointObjective = script.WaypointObjective

function startWalk()
	local textObjective = tostring(waypointObjective.Value)
	humanoid:MoveTo(wayPoints:FindFirstChild(textObjective).Position)
	if walkAnim.IsPlaying == false then
		walkAnim:Play()
		walkAnim:AdjustSpeed(0.63)
	end
humanoid.MoveToFinished:Wait()
print("Humanoid Finished Walking")
local magnitude = (character.Torso.Position - wayPoints:FindFirstChild(tostring(waypointObjective.Value)).Position).magnitude
if magnitude <= 10 then
if waypointObjective.Value ~= totalWaypoints.Value then
waypointObjective.Value = waypointObjective.Value + 1 -- Waypoint incrementor
end
hasReachedWaypoint.Value = true
retryCount.Value = 0
hasReachedWaypoint.Value = false
if waypointObjective.Value ~= totalWaypoints.Value then
startWalk()
else
		walkAnim:Stop()
	wait(2) -- Amount of seconds NPC waits at end point before teleporting back to the "Start" part
	waypointObjective.Value = 1
	character:MoveTo(start.Position)
    startWalk()
end
elseif retryCount.Value ~= 3 then
	retryCount.Value = retryCount.Value + 1
	startWalk()
else
	waypointObjective.Value = 1
	character:MoveTo(start.Position)
	retryCount.Value = 0
	hasReachedWaypoint.Value = false
	startWalk()
end
--[[if waypointObjective.Value == totalWaypoints.Value then -- Set the number in this statement to the number of waypoints (e.g num of waypoints + 1 excluding the "Start" point)
	walkAnim:Stop()
	wait(2) -- Amount of seconds NPC waits at end point before teleporting back to the "Start" part
	waypointObjective.Value = 1
	character:MoveTo(start.Position) -- Moves the character back to the start, don't need to change
    startWalk()
end]]
	end

startWalk()

Here’s a screenshot of the layout of my waypoints, incase this is any help:
Screenshot_1

And my value layout:
Screenshot_2

HasReachedWaypoint is a boolean value - Goes to true when waypoint is reached
RetryCount is a number value - If an NPC does not hit it’s target before the max wait time for Humanoid.MoveToFinsished, it will retry and increment the counter until it hits 3.
TotalWaypoints is self explanatory - The amount of waypoints in the model.
WaypointObjective - Also self explanatory - Current waypoint to go to.

Edit: Oops, images were tiny, fixed that.

Note: I forgot to mention the script is a LocalScript located in StarterPlayerScripts. Missed such an important detail, thanks for reminding me to add that @goldenstein64

So I have been testing your script myself in Studio, and made sure reproduce everything, down to the walk animations, and even tested it in-game, and it does exactly what it’s supposed to except for two things:

  • the NPC does not travel to the last waypoint in your model.
  • the NPC will not start moving unless i comment out those first three lines about waiting for the game to load

It’s super strange, but the fix is easy, just add 1 to your TotalWaypoints value, and comment out the first three lines. When this is done, it runs in studio and even in-game just fine.
Other than this, my best guess is that either the terrain you are testing this on has something to do with it, you have to restart/update studio for the script to work, or the place file the script is working in is just that different between yours and mine, which was probably why I had to comment out the first three lines.
I hope that helps.

That’s actually very strange, because when I’m close to the NPC, it walks just fine. It gets to the last waypoint as planned and teleports back to the start when it reaches the end waypoint.
I tried both of your suggestions but neither solved my issue, I’m so confused lmao.

Thanks for putting in the effort to reproduce it to test with, I do appreciate this.
Let me know if you try other things that help, I’ll look into it more myself tomorrow and try the most obscure things like you suggested.

Note; I must also add that the NPC walks fine if I make it walk via the command line with the Humanoid:MoveTo() function, but it has issues in my script? Very odd.

Where did you place your script, because that might make a difference in what it does? I placed my script in ServerScriptService just so you know.

Ah damn I should’ve mentioned this. It’s a LocalScript in StarterPlayerScripts.

Should’ve mentioned that, it’s quite important.
The NPC spawns locally for gameplay reasons but I’ve momentarily dragged it out into workspace so I can test my script. Maybe that’s interfered with this all? I’ll try tomorrow. Thanks for your help.

Aha! Now I know the problem!

LocalScripts used to move NPC’s only work if you have network ownership over them, which is true only when you are close enough to auto-obtain its network ownership!

I tested your script in a normal Script in ServerScriptService, which is why it worked so easily!

If you want this behavior to work from a local script, you have to call Part:SetNetworkOwner(player) on every part in the NPC from the server before letting the local script do its job.

You can either do this or instantiate the NPC locally, even though the NPC won’t replicate to the server and back to other clients.

3 Likes

Ahh thank you endlessly!
Setting the NetworkOwner happily solved my issue.
I only had to set it to the Torso because all other parts are connected, which I read means they will be affected too.

My NPC walks well with my system now working properly.

1 Like