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

Pathfinding 'lags' online?

Asked by
Marios2 360 Moderation Voter
8 years ago

I have this Pathfinding Script inside a character Model (NPC) which, for the time being, loops around the map. It works 100% in general.

However, online it seems to stop for a second before going to the next co-ordinate to the location, and in Studio it proceeds normally. How come?

local curnode = workspace.PathNodes.PathNode1 --Current location variable
local nextnode = workspace.PathNodes.PathNode2 --Next location variable

function getCurrentPos() --Get the current location
    for _,node in pairs(workspace.PathNodes:GetChildren()) do
        if (node.Position - script.Parent.Torso.Position).magnitude < 4 then curnode = node end
    end
end
function getNextPos() -Decide the next one based on your current
    if curnode == workspace.PathNodes.PathNode1 then
        local rnd = math.random(1,2)
        if rnd == 1 then nextnode = workspace.PathNodes.PathNode2 else nextnode = workspace.PathNodes.PathNode15 end --Yes, the nodes are a bit messy
    elseif curnode == workspace.PathNodes.PathNode2 then
        local rnd = math.random(1,2)
        if rnd == 1 then nextnode = workspace.PathNodes.PathNode10 else nextnode = workspace.PathNodes.PathNode11 end
    elseif curnode == workspace.PathNodes.PathNode3 then
        nextnode = workspace.PathNodes.PathNode4
    elseif curnode == workspace.PathNodes.PathNode4 then
        nextnode = workspace.PathNodes.PathNode5
    elseif curnode == workspace.PathNodes.PathNode5 then
        local rnd = math.random(1,2)
        if rnd == 1 then nextnode = workspace.PathNodes.PathNode13 else nextnode = workspace.PathNodes.PathNode6 end
    elseif curnode == workspace.PathNodes.PathNode6 then
        nextnode = workspace.PathNodes.PathNode7
    elseif curnode == workspace.PathNodes.PathNode7 then
        nextnode = workspace.PathNodes.PathNode8
    elseif curnode == workspace.PathNodes.PathNode8 then
        nextnode = workspace.PathNodes.PathNode9
    elseif curnode == workspace.PathNodes.PathNode9 then
        nextnode = workspace.PathNodes.PathNode10
    elseif curnode == workspace.PathNodes.PathNode10 then
        nextnode = workspace.PathNodes.PathNode14
    elseif curnode == workspace.PathNodes.PathNode11 then
        nextnode = workspace.PathNodes.PathNode7
    elseif curnode == workspace.PathNodes.PathNode12 then
        nextnode = workspace.PathNodes.PathNode8
    elseif curnode == workspace.PathNodes.PathNode13 then
        nextnode = workspace.PathNodes.PathNode16
    elseif curnode == workspace.PathNodes.PathNode14 then
        nextnode = workspace.PathNodes.PathNode1
    elseif curnode == workspace.PathNodes.PathNode15 then
        nextnode = workspace.PathNodes.PathNode3
    else nextnode = workspace.PathNodes.PathNode12
    end
end
function moveToPos() --Find the path to the destination and go for it
    for _,point in pairs(game:GetService("PathfindingService"):ComputeRawPathAsync(curnode.Position,nextnode.Position,130):GetPointCoordinates()) do
        script.Parent.Humanoid.WalkToPoint = point
        repeat wait() until (point - script.Parent.Torso.Position).magnitude < 3
    end
end

while game.Players.NumPlayers > 0 do --This loop is here so i can do more stuff later
    getCurrentPos()
    getNextPos()
    moveToPos()
end

--General reminder that this script works.

1 answer

Log in to vote
0
Answered by
KoreanBBQ 301 Moderation Voter
8 years ago

You could store the curnode values in a table and make this script easily half as long.

0
Doesn't answer the question. Comment instead - you are previleged to do that [5 rep] Marios2 360 — 8y
Ad

Answer this question