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

NPC PathFinding, How Can I Optimise This?

Asked by
Ruves 21
4 years ago

This is the first time I've ever worked with pathfinding and npcs but I went in and worked on it until I got results but when running it's clear that it's poor optimised. It does what it needs to do but is there any way I can put less stress on the server?

local pathFindingService = game:GetService("PathfindingService")

local humanoid = script.Parent.Humanoid
local body = script.Parent:FindFirstChild("HumanoidRootPart") or script.Parent:FindFirstChild("Torso")

local path = pathFindingService:CreatePath()

function ClothingStore()

    local path = pathFindingService:CreatePath()

    -- Enter Shop

    local cse = game.Workspace.clothingstoreenter
    local csl = game.Workspace.clothingstoreleave

    path:ComputeAsync(body.Position, cse.Position)

    local waypoints = path:GetWaypoints()

    for k, waypoint in pairs(waypoints) do
        humanoid:MoveTo(waypoint.Position)

        if waypoints.Action == Enum.PathWaypointAction.Jump then
            humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
        end

        humanoid.MoveToFinished:Wait()
    end

    body.CFrame = csl.CFrame

    -- Find Clothing

    local csp = game.Workspace.points:GetChildren()
    local cspd = csp[math.random(1, #csp)]

    path:ComputeAsync(body.Position, cspd.Position)

    local waypoints = path:GetWaypoints()

    for k, waypoint in pairs(waypoints) do
        humanoid:MoveTo(waypoint.Position)

        if waypoints.Action == Enum.PathWaypointAction.Jump then
            humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
        end

        humanoid.MoveToFinished:Wait()
    end

    -- Purchase Clothing

    wait(math.random(3, 7))

    local csc = game.Workspace.clothingstorecashpoint

    path:ComputeAsync(body.Position, csc.Position)

    local waypoints = path:GetWaypoints()

    for k, waypoint in pairs(waypoints) do
        humanoid:MoveTo(waypoint.Position)

        if waypoints.Action == Enum.PathWaypointAction.Jump then
            humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
        end

        humanoid.MoveToFinished:Wait()
    end

    -- Leave

    wait(math.random(3, 7))

    local cse = game.Workspace.clothingstoreenter
    local csl = game.Workspace.clothingstoreleave

    path:ComputeAsync(body.Position, csl.Position)

    local waypoints = path:GetWaypoints()

    for k, waypoint in pairs(waypoints) do
        humanoid:MoveTo(waypoint.Position)

        if waypoints.Action == Enum.PathWaypointAction.Jump then
            humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
        end

        humanoid.MoveToFinished:Wait()
    end

    body.CFrame = cse.CFrame

    -- Home

    local sp = game.Workspace.spawnpoint

    path:ComputeAsync(body.Position, sp.Position)

    local waypoints = path:GetWaypoints()

    for k, waypoint in pairs(waypoints) do
        humanoid:MoveTo(waypoint.Position)

        if waypoints.Action == Enum.PathWaypointAction.Jump then
            humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
        end

        humanoid.MoveToFinished:Wait()
    end

    script.Parent:remove()

end

ClothingStore()

Answer this question