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:
And my value layout:
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