local function n(M,h,P,L)if typeof(L)=="\073\110\115\116\097\110\099\101"and L:IsA("\066\097\115\101\080\097\114\116")then L=PhysicsService:GetCollisionGroupName(L.CollisionGroupId)end local V=Workspace.CurrentCamera local f={V}if typeof(P)=="\073\110\115\116\097\110\099\101"then table.insert(f,P)elseif typeof(P)=="\116\097\098\108\101"then for n,M in pairs(P)do if M~=V then table.insert(f,M)end end end local K=RaycastParams.new()K.FilterType=Enum.RaycastFilterType.Blacklist K.FilterDescendantsInstances=f K.IgnoreWater=true if L then K.CollisionGroup=L end local y=Workspace:Raycast(M,h,K)if not y then return nil,M+h end local t,O=y.Instance,y.Position if t and(not t.CanCollide and(not t:IsA("\083\101\097\116")or not t:IsA("\086\101\104\105\099\108\101\083\101\097\116")or t.Disabled))then table.insert(f,t)return n(O,h+(O-M),f,L)end return t,O end return n

local function FindCollidablePartOnRay(StartPosition,Direction,IgnoreList,CollisionGroup)
    --Convert the collision group.
    if typeof(CollisionGroup) == "Instance" and CollisionGroup:IsA("BasePart") then
        CollisionGroup = PhysicsService:GetCollisionGroupName(CollisionGroup.CollisionGroupId)
    end

    --Create the ignore list.
    local Camera = Workspace.CurrentCamera
    local NewIgnoreList = {Camera}
    if typeof(IgnoreList) == "Instance" then
        table.insert(NewIgnoreList,IgnoreList)
    elseif typeof(IgnoreList) == "table" then
        for _,Entry in pairs(IgnoreList) do
            if Entry ~= Camera then
                table.insert(NewIgnoreList,Entry)
            end
        end
    end

    --Create the parameters.
    local RaycastParameters = RaycastParams.new()
    RaycastParameters.FilterType = Enum.RaycastFilterType.Blacklist
    RaycastParameters.FilterDescendantsInstances = NewIgnoreList
    RaycastParameters.IgnoreWater = true
    if CollisionGroup then
        RaycastParameters.CollisionGroup = CollisionGroup
    end

    --Raycast and continue if the hit part isn't collidable.
    local RaycastResult = Workspace:Raycast(StartPosition,Direction,RaycastParameters)
    if not RaycastResult then
        return nil,StartPosition + Direction
    end
    local HitPart,EndPosition = RaycastResult.Instance,RaycastResult.Position
    if HitPart and not HitPart.CanCollide and (not HitPart:IsA("Seat") or not HitPart:IsA("VehicleSeat") or HitPart.Disabled) then
        table.insert(NewIgnoreList,HitPart)
        return FindCollidablePartOnRay(EndPosition,Direction + (EndPosition - StartPosition),NewIgnoreList,CollisionGroup)
    end

    --Return the hit result.
    return HitPart,EndPosition
end

return FindCollidablePartOnRay
