Pathfinding NPCs Lag in Game, how do i fix?
I've seen many players reporting this problem but i can never seem to update my script to fix this problem. When NPCs are wandering, they always seem to stop and start moving, along with a short pause of animation.
some people say things about humanoid.MoveToFinished:Wait()
but i'm not sure if it has anything to do with the script i'm using.
Here is the script I am using, be warned it's long.
001 | local players = Info.Players:GetPlayers() |
002 | local closestCharacter, closestCharacterDistance |
005 | local player = players [ i ] |
007 | if player.Neutral or player.TeamColor ~ = Settings.FriendlyTeam.Value then |
008 | local character = player.Character |
010 | if character ~ = nil and character:FindFirstChild( 'Humanoid' ) ~ = nil and character.Humanoid:IsA( 'Humanoid' ) then |
011 | local distance = player:DistanceFromCharacter(Monster:GetCFrame().p) |
013 | if distance < Monster:GetMaximumDetectionDistance() then |
014 | if closestCharacter = = nil then |
015 | closestCharacter, closestCharacterDistance = character, distance |
017 | if closestCharacterDistance > distance then |
018 | closestCharacter, closestCharacterDistance = character, distance |
027 | if closestCharacter ~ = nil then |
028 | Mind.CurrentTargetHumanoid.Value = closestCharacter.Humanoid |
032 | function Monster:TryRecomputePath() |
033 | if Data.AutoRecompute or tick() - Data.LastRecomputePath > 1 /Info.RecomputePathFrequency then |
034 | Monster:RecomputePath() |
038 | function Monster:GetTargetCFrame() |
039 | local targetHumanoid = Mind.CurrentTargetHumanoid.Value |
041 | if Monster:TargetIsValid() then |
042 | return targetHumanoid.Torso.CFrame |
048 | function Monster:IsAlive() |
049 | return Self.Humanoid.Health > 0 and Self.Humanoid.Torso ~ = nil |
052 | function Monster:TargetIsValid() |
053 | local targetHumanoid = Mind.CurrentTargetHumanoid.Value |
055 | if targetHumanoid ~ = nil and targetHumanoid:IsA 'Humanoid' and targetHumanoid.Torso ~ = nil and targetHumanoid.Torso:IsA 'BasePart' then |
062 | function Monster:HasClearLineOfSight() |
064 | local myPos, targetPos = Monster:GetCFrame().p, Monster:GetTargetCFrame().p |
066 | local hit, pos = Workspace:FindPartOnRayWithIgnoreList( |
073 | Mind.CurrentTargetHumanoid.Value.Parent |
085 | function Monster:RecomputePath() |
086 | if not Data.Recomputing then |
087 | if Monster:IsAlive() and Monster:TargetIsValid() then |
088 | if Monster:HasClearLineOfSight() then |
089 | Data.AutoRecompute = true |
091 | Monster:GetCFrame().p, |
092 | Monster:GetTargetCFrame().p |
095 | Data.LastRecomputePath = tick() |
096 | Data.CurrentNode = nil |
097 | Data.CurrentNodeIndex = 2 |
100 | Data.Recomputing = true |
101 | Data.AutoRecompute = false |
104 | local path = Info.PathfindingService:ComputeSmoothPathAsync( |
105 | Monster:GetCFrame().p, |
106 | Monster:GetTargetCFrame().p, |
109 | Data.PathCoords = path:GetPointCoordinates() |
112 | Data.Recomputing = false |
113 | Data.LastRecomputePath = tick() |
114 | Data.CurrentNode = nil |
115 | Data.CurrentNodeIndex = 1 |
121 | function Monster:Update() |
122 | Monster:ReevaluateTarget() |
123 | Monster:SearchForTarget() |
124 | Monster:TryRecomputePath() |
128 | function Monster:TravelPath() |
129 | local closest, closestDistance, closestIndex |
130 | local myPosition = Monster:GetCFrame().p |
131 | local skipCurrentNode = Data.CurrentNode ~ = nil and (Data.CurrentNode - myPosition).magnitude < 3 |
133 | for i = Data.CurrentNodeIndex, #Data.PathCoords do |
134 | local coord = Data.PathCoords [ i ] |
135 | if not (skipCurrentNode and coord = = Data.CurrentNode) then |
136 | local distance = (coord - myPosition).magnitude |
138 | if closest = = nil then |
139 | closest, closestDistance, closestIndex = coord, distance, i |
141 | if distance < closestDistance then |
142 | closest, closestDistance, closestIndex = coord, distance, i |
153 | Data.CurrentNode = closest |
154 | Data.CurrentNodeIndex = closestIndex |
156 | local humanoid = Self:FindFirstChild 'Humanoid' |
158 | if humanoid ~ = nil and humanoid:IsA 'Humanoid' then |
159 | humanoid:MoveTo(closest) |
162 | if Monster:IsAlive() and Monster:TargetIsValid() then |
163 | Monster:TryJumpCheck() |
167 | if closestIndex = = #Data.PathCoords then |
169 | Data.AutoRecompute = true |
Hope there is something that can be done about this.
Also, sorry for the long script again.