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

Is there any way that I can improve this Pathfinding AI for a better server performance?

Asked by 6 years ago
Edited 6 years ago

So this is the script that I created for the AI

local Pathfinding = game:GetService("PathfindingService")
local NPC = script.Parent
local Hum = NPC.Harmoid
local RootPart = NPC.HumanoidRootPart
local RunAnim = Hum:LoadAnimation(NPC.RunAnim)
local IdleAnim = Hum:LoadAnimation(NPC.IdleAnim)
local RootChase = nil
local HumChase = nil
local Plr = nil
local PlayerList = game.Players
local function Move()
    local pathfinder = Pathfinding:FindPathAsync(RootPart.Position,RootChase.Position)
        local way = pathfinder:GetWaypoints()
        for i,v in pairs(way) do
            Hum:MoveTo(v.Position)
            Hum.MoveToFinished:wait()
        end
end
local function Mag(point)
    return (RootPart.Position - point.Position).magnitude
end
IdleAnim:Play()

while true do wait(5)
    local Lowest = math.huge
    for _,plr in pairs(PlayerList:GetPlayers()) do
        if plr.Character then
            local pchar = plr.Character
            local phum = pchar.Humanoid
            if phum.Health > 0 then
                local proot = pchar.HumanoidRootPart
                local Mager = Mag(proot)
                if Mager<Lowest then
                    Lowest = Mager
                    RootChase = proot
                    HumChase = phum
                    Plr = plr
                end
            end
        end
    end
    RunAnim:Play()
    while Plr do
        print('Activating!')
        Move()
    end
    RunAnim:Stop()
end

That's everything :P

First thing is: At the printing function. It activates more than 30 times O_O

Second thing is: at the Hum.MoveToFinished:wait() part it has a slow delay and glitchy.

0
if you don't want it to print more than 30 times just add a debounce. http://wiki.roblox.com/index.php?title=Debounce wookey12 174 — 6y
0
Then what about the second thing? magicguy78942 238 — 6y
0
idk. look on the wiki wookey12 174 — 6y

Answer this question