โค้ด Roblox
2022-09-14 07:55

สคริป Roblox : AI Path-Finder

local pfs = game:GetService("PathfindingService")
local points = {}
local path = pfs:CreatePath()
local PointIdx = 1
local char = script.Parent
local hum = char.Humanoid
local hrp = char:WaitForChild("HumanoidRootPart")
local gold = game.Workspace.Gold

--look for shortest path
local function followPath(goal)
path:ComputeAsync(hrp.Position, goal.Position)
points = {}
if path.Status == Enum.PathStatus.Success then
points = path:GetWaypoints()
PointIdx = 1
hum:MoveTo(points[PointIdx].Position)
else
print("no path available!!!")
end
end

--move function
hum.MoveToFinished:Connect(function(reached)
if reached and PointIdx < #points then
PointIdx += 1
if points[PointIdx].Action == Enum.PathWaypointAction.Jump then
hum.Jump = true
end
hum:MoveTo(points[PointIdx].Position)
end
end)

--recommpute our path
path.Blocked:Connect(function(blockedPointIdx)
if blockedPointIdx > PointIdx then
followPath(gold)
end
end)

wait(6)
followPath(gold)