Hello developers.
I am making the pathfinding in module script. I want to use GetPropertyChangedSignal Position of player character for head to change the position for path, but i don’t know, it is not working. Any problems in my code?
// Module Script
-- --!nocheck
--[[
Advanced PathfindingService. ver 1.0 BETA
]]
local path = {}
local Pathfinding = game:GetService("PathfindingService")
local TestService = game:GetService("TestService")
path.DebugMode = false -- Recommended use for testing bugs, do not use for no way, that may doing lags in your game.
path.ViewWayPoints = true -- Recommended use for testing path.
path.__index = path
local WarningMessage = "Warning Message: %s"
function path.new()
return setmetatable({
-- Default Pathfinding Parameters --
AgentRadius = 2,
AgentHeight = 5,
AgentCanJump = false,
WaypointSpacing = 4,
Costs = nil,
--
Retrying = false,
path = nil,
terminated = false,
Raycast = true,
MaxDistance = 100,
},path)
end
function DebugMessage(self,string)
if self.DebugMode then
TestService:Error(string)
end
end
function MoveToPart(self,pos1:Vector3,pos2:Vector3,humanoid:Humanoid,AlterReached:FunctionalTest?)
local connection
local PathBlocked
self.terminated = false
if self.path.Status == Enum.PathStatus.Success then
local waypoints = self.path:GetWaypoints()
if self.ViewWayPoints then
local ViewWayPoint = workspace:FindFirstChild("ViewWayPoints") or Instance.new("Folder",workspace)
ViewWayPoint.Name = "ViewWayPoints"
for i, v in pairs(waypoints) do
local part = Instance.new("Part",ViewWayPoint)
part.Shape = Enum.PartType.Ball
part.Anchored = true
part.CanCollide = false
part.BrickColor = BrickColor.new("Neon orange")
part.Material = Enum.Material.Neon
part.Size = Vector3.new(0.5,0.5,0.5)
part.Position = v.Position
end
end
connection = humanoid.MoveToFinished:Connect(function(reached)
if reached and self.nextWaypointIndex <= #waypoints and not self.terminated and self.path then
if self.nextWaypointIndex then
humanoid:MoveTo(waypoints[self.nextWaypointIndex].Position)
if waypoints[self.nextWaypointIndex].Action == Enum.PathWaypointAction.Jump then
humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
end
self.nextWaypointIndex += 1
end
else
if self.nextWaypointIndex >= #waypoints then
if AlterReached then
task.spawn(AlterReached)
end
end
connection:Disconnect()
end
end)
PathBlocked = self.path.Blocked:Connect(function(index)
if index >= self.nextWaypointIndex then
connection:Disconnect()
PathBlocked:Disconnect()
self.path:ComputeAsync(pos1.Position,pos2)
if workspace:FindFirstChild("ViewWayPoints") then workspace["ViewWayPoints"]:Destroy() end
MoveToPart(self,pos1,pos2,humanoid,AlterReached)
end
end)
if not self.terminated then
self.nextWaypointIndex = 2
if self.nextWaypointIndex then
if waypoints[self.nextWaypointIndex] then
humanoid:MoveTo(waypoints[self.nextWaypointIndex].Position)
self.nextWaypointIndex += 1
end
end
end
return {connection,PathBlocked}
else
if self.Retrying then
while task.wait() do
local success, mistake = pcall(function()
self.path:ComputeAsync(pos1.Position,pos2)
end)
if not success then DebugMessage(self,"Something went wrong with creating ComputeAsync! Erorr Message: "..mistake) end
if self.path.Status == Enum.PathStatus.Success then break end
end
if connection and PathBlocked then
connection:Disconnect()
PathBlocked:Disconnect()
end
MoveToPart(self,pos1,pos2,humanoid,AlterReached)
else
if self.DebugMode then
warn(WarningMessage:format("Cannot to find path, retrying is disabled. Move terminated."))
return "terminated"
end
end
end
end
function path:terminate()
self.terminated = true
self.path = nil
end
function path:MoveTo(pos1 : BasePart, pos2 : BasePart, Humanoid : Humanoid, AlterReached : FunctionalTest?)
local connections
local connection
if not self.path then
if (pos1.Position - pos2.Position).Magnitude <= self.MaxDistance then
if self.Raycast then
local ray = Ray.new(pos1.Position,pos2.Position-pos1.Position)
local hit, position = workspace:FindPartOnRay(ray,pos1,false)
if hit then
if hit.Name == pos2.Name then else
return
end
else
return
end
end
self.path = Pathfinding:CreatePath({
AgentRadius = self.AgentRadius,
AgentHeight = self.AgentHeight,
AgentCanJump = self.AgentCanJump,
WaypointSpacing = self.WaypointSpacing,
Costs = self.Costs
})
if self.path then
local success, mistake = pcall(function()
self.path:ComputeAsync(pos1.Position,pos2.Position)
connections = MoveToPart(self,pos1,pos2.Position,Humanoid,AlterReached)
end)
if not success then DebugMessage(self,"Something went wrong with creating ComputeAsync! Error Message: "..mistake) end
else
DebugMessage(self,"Something went wrong with Path!")
end
connection = pos2:GetPropertyChangedSignal("Position"):Connect(function()
if connections then
connections[1]:Disconnect()
connections[2]:Disconnect()
end
if workspace:FindFirstChild("ViewWayPoints") then workspace["ViewWayPoints"]:Destroy() end
if self.path then
local success, mistake = pcall(function()
self.path:ComputeAsync(pos1.Position,pos2.Position)
if pos2 then
connections = MoveToPart(self,pos1,pos2.Position,Humanoid,AlterReached)
if connections == "terminated" or self.terminated then
connection:Disconnect()
Humanoid.TargetPoint = pos1.Position
end
end
end)
if not success then DebugMessage(self,"Something went wrong with creating ComputeAsync! Error Message: "..mistake) end
else
DebugMessage(self,"Something went wrong with Path!")
end
end)
end
else
if self.DebugMode then
warn(WarningMessage:format("Feature to change the point position when moving is actived, is currently in testing phase. For now, you need to terminate the moving."))
end
end
end
return path
// Server Script
local AdvancedPathFinding = require(game.ReplicatedStorage.AdvancedPathFinding)
AdvancedPathFinding.DebugMode = true
local path = AdvancedPathFinding.new()
path.Retrying = true
path.AgentCanJump = false
path.Raycast = false
function Detect()
local List = workspace:GetDescendants()
for i, v in pairs(List) do
if game.Players:FindFirstChild(v.Name) then
local Head = v:FindFirstChild("Head")
if Head then
path:MoveTo(script.Parent.HumanoidRootPart,Head,script.Parent.Humanoid,function()
print("catched!")
end)
end
end
end
end
while task.wait() do
Detect()
end
