Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Pathfinding NPCs Lag in Game, how do i fix?

Asked by 6 years ago

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
003 
004    for i=1, #players do
005        local player = players[i]
006 
007        if player.Neutral or player.TeamColor ~= Settings.FriendlyTeam.Value then
008            local character = player.Character
009 
010            if character ~= nil and character:FindFirstChild('Humanoid') ~= nil and character.Humanoid:IsA('Humanoid') then
011                local distance = player:DistanceFromCharacter(Monster:GetCFrame().p)
012 
013                if distance < Monster:GetMaximumDetectionDistance() then
014                    if closestCharacter == nil then
015                        closestCharacter, closestCharacterDistance = character, distance
View all 172 lines...

Hope there is something that can be done about this. Also, sorry for the long script again.

0
Same i figured out it takes some time for it to recalculate a path to a player for my npc it stops more the closer it gets to the player ...stops as in kinda bumpy stopping 129Steve129 7 — 6y
0
Exactly, but my NPCs stop when trying to walk around solid parts, if the player were to be in a straight line from the NPC without any obstacles, then it will not lag mrfrank79 30 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago

Just type this

01local h = script.Parent.Humanoid
02local torso = script.Parent.UpperTorso --Torso if Mob is r6 UpperTorso if Mob is r15
03local spawnCF = torso.CFrame
04function findPlayer()
05 for _,v in next, game.Players:GetPlayers() do
06if v.Character then
07local char = v.Character if char:FindFirstChild("Humanoid") and char:FindFirstChild("UpperTorso") then --Torso if Player is r6 UpperTorso if Player is r15
08local ptorso = char.UpperTorso --Torso if Player is r6 UpperTorso if Player is r15
09if (ptorso.Position - torso.Position).magnitude <= 20 then
10 return v
11end
12end
13 end
14end
15return nil
View all 24 lines...
Ad

Answer this question