local n=4 local U=false local G={}local y local h=game:GetService("\080\108\097\121\101\114\115")local d=RaycastParams.new()d.FilterType=Enum.RaycastFilterType.Blacklist d.IgnoreWater=true G.CastToTarget=function(G,h,q,X,a,s)local V local Z=false local A local O=0 local W=G if s then V=s else V=d end if not X then X=4000 end local i=(h-G).Unit while O<n and not Z do O=O+1 d.FilterDescendantsInstances=a if(W-G).Magnitude>X then break end A=workspace:Raycast(G,i*X,d)if A then y=A end if A and U then local n=Instance.new("\080\097\114\116")n.Shape=Enum.PartType.Ball n.Size=Vector3.new(.4,.4,.4)n.Anchored=true n.Position=A.Position n.CanCollide=false n.Transparency=.5 n.Color=Color3.new(1,0,0)n.Name="\068\101\098\117\103"n.Parent=workspace table.insert(a,n)end if not A then break elseif A.Instance:IsA("\084\101\114\114\097\105\110")then Z=true elseif A.Instance:IsA("\066\097\115\101\080\097\114\116")and not A.Instance.CanCollide then G=A.Position table.insert(a,A.Instance)elseif A.Instance:IsA("\066\097\115\101\080\097\114\116")and A.Instance.CanCollide or A.Instance.Name=="\065\114\109\111\114"then Z=true end end if not Z then if(G-h).Magnitude<q and y then return y else return nil end else if(A.Position-h).Magnitude<q then return A end end end return G
-- Config variables
local castLimit = 4 -- Number of times the module will recast if a non collided part is hit
local castDebug = false -- Creates red dots where raycasts hit

local dir = {}
local lastResult
local players = game:GetService("Players")

local defaultParams = RaycastParams.new()
defaultParams.FilterType = Enum.RaycastFilterType.Blacklist
defaultParams.IgnoreWater = true

dir.CastToTarget = function(origin, target, tolerance, distLimit, ignore, params)
	local cParams
	local hit = false
	local result
	local castCount = 0
	local firstOrigin = origin

	-- Set new arguments
	if params then
		cParams = params
	else
		cParams = defaultParams
	end

	if not distLimit then
		distLimit = 4000
	end

	-- Loop cast until target hit or hit nil
	local direction = (target - origin).Unit
	while castCount < castLimit and not hit do
		castCount = castCount + 1

		defaultParams.FilterDescendantsInstances = ignore

		-- Too far away
		if (firstOrigin - origin).Magnitude > distLimit then
			break
		end

		result = workspace:Raycast(origin, direction * distLimit, defaultParams)
		if result then
			lastResult = result
		end

		if result and castDebug then
			--print("Cast hit "..result.Instance.Name)
			local dPart = Instance.new("Part")
			dPart.Shape = Enum.PartType.Ball
			dPart.Size = Vector3.new(0.4, 0.4, 0.4)
			dPart.Anchored = true
			dPart.Position = result.Position
			dPart.CanCollide = false
			dPart.Transparency = 0.5
			dPart.Color = Color3.new(1, 0, 0)
			dPart.Name = "Debug"
			dPart.Parent = workspace
			table.insert(ignore, dPart)
		end

		if not result then
			-- Hit nil!
			break
		elseif result.Instance:IsA("Terrain") then
			-- Hit terrain
			hit = true
		elseif result.Instance:IsA("BasePart") and not result.Instance.CanCollide then
			-- Hit a part without collisions, recast
			origin = result.Position
			table.insert(ignore, result.Instance)
		elseif (result.Instance:IsA("BasePart") and result.Instance.CanCollide) or result.Instance.Name == "Armor" then
			-- Hit a part with collisions!
			hit = true
		end
	end

	if not hit then
		if (origin - target).Magnitude < tolerance and lastResult then
			return lastResult
		else
			return nil
		end
	else
		if (result.Position - target).Magnitude < tolerance then
			return result
		end
	end
end

return dir
