Module:
local PathfindingModule = {}
function PathfindingModule.CreatePath (Script: Script, Character: Model, EndPosition: Vector3, CostsTable: {any}, CanClimb: boolean, CanJump: boolean, Radius: number, MoveToPosition: boolean, ShowPath: boolean, PathVisibleTime: number, DestroyValue: boolean, IsGui: boolean)
if not Character:IsA("Model") then warn("Selected Instance is not a Model.") return end
if Character == nil or not Character:FindFirstChildWhichIsA("Humanoid") or EndPosition == nil or Character.PrimaryPart == nil then warn("Invalid Character Properties") return end
if DestroyValue == nil then DestroyValue = true end
if CanClimb == nil then CanClimb = false end
if CanJump == nil then CanJump = false end
if ShowPath == nil then ShowPath = false end
if Script == nil and ShowPath == true then warn("No script provided to store value.") return end
if MoveToPosition == nil then MoveToPosition = true end
if IsGui == nil then IsGui = false end
if Radius == nil then Radius = 2 end
if CostsTable == nil then CostsTable = {} end
if ShowPath == true then
local PathParent = Instance.new("Model")
PathParent.Parent = workspace
PathParent.Name = 'Path'
local Value = Instance.new("ObjectValue", Script)
Value.Name = 'Model'
Value.Value = PathParent
end
local PathfindingService = game:GetService("PathfindingService")
local HumanoidRootPart = Character.PrimaryPart
local Humanoid = Character:FindFirstChildWhichIsA("Humanoid")
local Path = PathfindingService:CreatePath({
AgentRadius = Radius,
AgentCanJump = CanJump,
AgentCanClimb = CanClimb,
Costs = CostsTable
})
local success, errorMessage = pcall(function()
Path:ComputeAsync(HumanoidRootPart.Position, EndPosition)
end)
if success and Path.Status == Enum.PathStatus.Success and MoveToPosition then
local NumberRepeated = 0
for _, Waypoint in pairs(Path:GetWaypoints()) do
NumberRepeated += 1
if ShowPath == true then
local Part = Instance.new("Part")
Part.Anchored = true
Part.CanCollide = false
if not IsGui then
Part.Transparency = 0
Part.Shape = Enum.PartType.Ball
Part.Material = Enum.Material.Neon
else
Part.Transparency = 1
local Gui = Instance.new("BillboardGui", Part)
Gui.Size = UDim2.fromScale(1, 1)
local Frame = Instance.new("Frame", Gui)
Frame.Size = UDim2.fromScale(1, 1)
Frame.Transparency = 0
Frame.BackgroundColor3 = Color3.new(1, 1, 1)
local Corner = Instance.new("UICorner", Frame)
Corner.CornerRadius = UDim.new(100, 0)
end
Part.Size = Vector3.new(1, 1, 1)
Part.CastShadow = false
Part.Position = Waypoint.Position + Vector3.new(0, 1, 0)
Part.Name = 'Waypoint' .. tostring(NumberRepeated)
Part.Parent = Script:FindFirstChild("Model").Value
Part:SetAttribute("WaypointNumber", NumberRepeated)
if NumberRepeated == #Path:GetWaypoints() then
NumberRepeated = 0
if DestroyValue == true and ShowPath == true then
local Value = Script:FindFirstChild("Model")
if PathVisibleTime ~= -1 and PathVisibleTime ~= nil then
repeat task.wait() until #Value.Value:GetChildren() <= 1
Value.Value:Destroy()
end
Value:Destroy()
end
end
if PathVisibleTime ~= nil and PathVisibleTime ~= -1 then
task.delay(PathVisibleTime, function()
Part:Destroy()
end)
end
end
Humanoid:MoveTo(Waypoint.Position)
if Waypoint.Action == Enum.PathWaypointAction.Jump and CanJump == true then
Humanoid.Jump = true
end
Humanoid.MoveToFinished:Wait()
end
elseif errorMessage ~= nil and MoveToPosition then
warn('Failed because ' .. errorMessage)
elseif success and Path.Status ~= Enum.PathStatus.Success and errorMessage == nil and MoveToPosition then
warn('Pathfinding Failed for Unknown Reason.')
elseif success and Path.Status == Enum.PathStatus.Success and not MoveToPosition then
return Path
end
end
return PathfindingModule
This was me testing my scripting knowledge and it worked!
Any feedback is appreciated!!! ![]()