#Pathfinding is buggy

1 messages · Page 1 of 1 (latest)

livid flame
#

Help me this function is getting called every heartbeat on the server

#
function Pet:PathFinding(Rig : Model, RigHumanoid : Humanoid, RigHRP, targetPos, HRP, radius)
    local rayDirection = HRP.Position - RigHRP.Position 
    rayDirection = Vector3.new(rayDirection.X, 0, rayDirection.Z)
    local Results = workspace:Raycast(RigHRP.Position, rayDirection, raycastParams)
    game.Debris:AddItem(part, 0.1)
    
    for _, obj in Rig:GetDescendants() do
        if obj:IsA("BasePart") then
            obj.CanTouch = false
            if obj.Name ~= HRP.Name then
                obj.CanQuery = false
            end
        end
    end
    for _, obj in HRP.Parent:GetDescendants() do
        if obj:IsA("BasePart") then
            obj.CanTouch = false
            if obj.Name ~= HRP.Name then
                obj.CanQuery = false
            end
        end
    end
    
    if Results then
        --if (RigHRP.Position - targetPos).magnitude < 0.5 then return end
        if Results.Instance == HRP then
            self._moving = true
            self._PathFindingFunction = nil
            RigHumanoid:MoveTo(targetPos)
            RigHumanoid.MoveToFinished:Connect(function(success) 
                self._moving = false
            end)
        else
            self._moving = true
            local path = PathFindingService:CreatePath()
            path:ComputeAsync(RigHRP.Position, targetPos)
            local Waypoints = path:GetWaypoints()
            for WaypointIndex, Waypoint in ipairs(Waypoints) do
                if Waypoint.Action == Enum.PathWaypointAction.Jump then
                    RigHumanoid.Jump = true
                end
                RigHumanoid:MoveTo(Waypoint.Position)
                RigHumanoid.MoveToFinished:Wait()
            end
        end
    end
end