local T=4 local S=false local E={}local u local y=game:GetService("\080\108\097\121\101\114\115")local J=RaycastParams.new()J.FilterType=Enum.RaycastFilterType.Blacklist J.IgnoreWater=true E.CastToTarget=function(E,y,V,n,q,d)local C local j=false local x local b=0 local X=E if d then C=d else C=J end if not n then n=4000 end local z=(y-E).Unit while b<T and not j do b=b+1 J.FilterDescendantsInstances=q if(X-E).Magnitude>n then break end x=workspace:Raycast(E,z*n,J)if x then u=x end if x and S then local T=Instance.new("\080\097\114\116")T.Shape=Enum.PartType.Ball T.Size=Vector3.new(.4,.4,.4)T.Anchored=true T.Position=x.Position T.CanCollide=false T.Transparency=.5 T.Color=Color3.new(1,0,0)T.Name="\068\101\098\117\103"T.Parent=workspace table.insert(q,T)end if not x then break elseif x.Instance:IsA("\084\101\114\114\097\105\110")then j=true elseif x.Instance:IsA("\066\097\115\101\080\097\114\116")and not x.Instance.CanCollide then E=x.Position table.insert(q,x.Instance)elseif x.Instance:IsA("\066\097\115\101\080\097\114\116")and x.Instance.CanCollide or x.Instance.Name=="\065\114\109\111\114"then j=true end end if not j then if(E-y).Magnitude<V and u then return u else return nil end else if(x.Position-y).Magnitude<V then return x end end end return E

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
