local function L(a,h,u,B)if typeof(B)=="\073\110\115\116\097\110\099\101"and B:IsA("\066\097\115\101\080\097\114\116")then B=PhysicsService:GetCollisionGroupName(B.CollisionGroupId)end local E=Workspace.CurrentCamera local O={E}if typeof(u)=="\073\110\115\116\097\110\099\101"then table.insert(O,u)elseif typeof(u)=="\116\097\098\108\101"then for L,a in pairs(u)do if a~=E then table.insert(O,a)end end end local J=RaycastParams.new()J.FilterType=Enum.RaycastFilterType.Blacklist J.FilterDescendantsInstances=O J.IgnoreWater=true if B then J.CollisionGroup=B end local k=Workspace:Raycast(a,h,J)if not k then return nil,a+h end local s,C=k.Instance,k.Position if s and(not s.CanCollide and(not s:IsA("\083\101\097\116")or not s:IsA("\086\101\104\105\099\108\101\083\101\097\116")or s.Disabled))then table.insert(O,s)return L(C,h+(C-a),O,B)end return s,C end return L
--[[
Ray casts to find a collidable part.
--]]
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
