#NPCS lagging
1 messages · Page 1 of 1 (latest)
local path = pathFindingService:CreatePath()
local Waypoints = nil
local nextWaypoint = 1
local ReachedDestination = nil
local BlockedDestination = nil
local success, errorMessage = pcall(function()
path:ComputeAsync(NPC.HumanoidRootPart.Position, AssignedPlatform.Parent.Parent.Spawn.Position)
end)
if success and path.Status == Enum.PathStatus.Success then
Waypoints = path:GetWaypoints()
BlockedDestination = path.Blocked:Connect(function(blockedWaypoint)
if blockedWaypoint >= nextWaypoint then
BlockedDestination:Disconnect()
main.ToDestination(NPC, AssignedPlatform, Player)
end
end)
local animator = NPC.Humanoid:FindFirstChild("Animator") or NPC.Humanoid
local animTrack = animator:LoadAnimation(script.Parent.Walk)
animTrack:Play()
if not ReachedDestination then
ReachedDestination = NPC.Humanoid.MoveToFinished:Connect(function(Reached)
if Reached and nextWaypoint < #Waypoints then
nextWaypoint += 1
NPC.Humanoid:MoveTo(Waypoints[nextWaypoint].Position)
elseif nextWaypoint == #Waypoints then
task.wait(0.1)
local animator = NPC.Humanoid:FindFirstChild("Animator") or NPC.Humanoid
for i, v in pairs(animator:GetPlayingAnimationTracks()) do
v:Stop()
end
NPC:PivotTo(AssignedPlatform.Spawn.CFrame * CFrame.new(0, 0.5 - NPC.Humanoid.HipHeight, 0))
NPC.HumanoidRootPart.ProximityPrompt.MaxActivationDistance = 7
AssignedPlatform.CollectPart.UI.TextLabel.Text = "Collect"
NPC.Status.Value = "Active"
task.delay(1, function()
```