Hi! Earlier today I was debugging my NPC I’ve made and I realized that when bumped into it enough times, my NPC decides to have long durations of moving pauses when it has reached the current waypoint, until it just stops moving entirely, and then starts moving slowly again.
Basically, the target has a cylinder welded to it’s HumanoidRootPart and the NPC keeps circling around random areas right outside of the cylinder. That’s what it’s supposed to do.
(Important Side note, the pathfinding WaypointSpacing is 5 and the pathfinding states that the Cylinder Part cost is math.huge.)
Here’s a fraction of the script:
while wait() do
Target = CheckForTarget() -- check for target is the function that filters every model in the workspace to see if theres a target near enemy and returns that target
if Target then
local SetupWaypointCFrame = CFrame.new(Cylinder.Position) * CFrame.Angles(0, math.random(0, 360), 0) -- Cylinder is defined on CheckForTarget(), it's a part welded to the target (It's the cylinder seen on the video)
local WaypointCFrame = SetupWaypointCFrame + (SetupWaypointCFrame.LookVector * Vector3.new(Cylinder.Size.Z - 5, 0, Cylinder.Size.Z - 5))
GetPath(WaypointCFrame.Position) --GetPath computes a path for the enemy, the parameter being the destination
local FirstWaypoint = true
for i, v in pairs(Path:GetWaypoints()) do
--[[local part = Instance.new("Part", workspace) -- THIS SETS THE WAYPOINT PART
part.Anchored = true
part.Size = Vector3.new(1,1,1)
part.Position = v.Position]]--
if FirstWaypoint == true then --irrelevant code but important to know
FirstWaypoint = false
else
--WHERE I SUSPECT THE PROBLEM LIES
script.Parent.Humanoid:MoveTo(v.Position)
local Moving = true
repeat wait()
print(Moving) -- When the NPC reaches the point where it doesn't move anymore, I realize this starts only printing true, even when it's not moving
Target = CheckForTarget()
if Target then
if script.Parent.HumanoidRootPart.AlignOrientation.Enabled == false then -- irrelevant code but important to know
script.Parent.HumanoidRootPart.AlignOrientation.Enabled = true
end
if script.Parent.Humanoid.AutoRotate == true then -- irrelevant code but important to know
script.Parent.Humanoid.AutoRotate = false
end
script.Parent.HumanoidRootPart.AlignOrientation.CFrame = CFrame.new(script.Parent.HumanoidRootPart.Position, Target.Parent:FindFirstChild("HumanoidRootPart").Position) -- irrelevant code but important to know
end
script.Parent.Humanoid.MoveToFinished:Connect(function()
Moving = false
end)
until Target == nil or TargetPosition ~= Target.Parent:FindFirstChild("HumanoidRootPart").Position or Moving == false
-- Target and TargetPosition are all defined on CheckForTarget()
--
end
--part:Destroy()
end
end
end
Here’s a video demonstrating the issue (Sorry it’s 2 minutes long, NPC was being uncooperative + I also demonstrate his starting behavior, which is what I would like the NPC to keep behaving like)
notice how it progressively gets slower at some point and when I bump into it one last time before it just stops moving, and then starts slowly moving again.
something I tried was replacing
script.Parent.Humanoid.MoveToFinished:Connect(function()
Moving = false
end)
with
if script.Parent.Humanoid.MoveDirection.Magnitude <= 0 then
Moving = false
end
and it gave me another issue, here’s what I’m talking about:
Is there any way to either fix the NPC slowing down and stopping issue or remind the NPC to not cross the cylinder (which detail, has a pathfinder modifier with the cost of math.huge)?
help