Smart Raycast[Open source]

Hello . I made a module called SmartRaycast . The difference between SmartRayCast and regular raycast is that SmartRaycast have 3 different mode. Mode 1 is IgnoreCannotCollide , Mode 2 is IgnoreTransparent and mode 3 is IgnoreTranslucent. Here is the script:

local Settings = {
	TranslucentTranparency = 0.5,
}

local module = {
	Mod = {
		IgnoreCannotCollide = 1,
		IgnoreTransparent = 2,
		IgnoreTranslucent = 3,
	},
}

function module:RayCast(origin,direction,prams,mod)
	if not origin or typeof(origin) ~= "Vector3" then warn("Argument 1 nil: Origin must be a vector3") return end
	if not direction or typeof(direction) ~= "Vector3" then warn("Argument 2 nil: Direction must be a vector3") return end
	if not prams or typeof(prams) ~= "RaycastParams" then warn("Argument 3 nil: Direction must be a RaycastParams") return end
	local function CastRay()
		local ray = workspace:Raycast(origin,direction,prams)
		return ray
	end
	
	local ray
	local blacklistTable = prams.FilterDescendantsInstances
	repeat
		wait()
		prams.FilterDescendantsInstances = blacklistTable
		ray = CastRay()
		if ray == nil then break end
		if mod == nil then break end
		if mod == 1 and ray.Instance.CanCollide == false then
			table.insert(blacklistTable,ray.Instance)
			elseif mod == 1 then
			break
		end
		print(ray.Instance.Transparency)
		if mod == 2 and ray.Instance.Transparency == 1 then
			table.insert(blacklistTable,ray.Instance)
		elseif mod == 2 then
			break
		end
		if mod == 3 and ray.Instance.Transparency >= Settings.TranslucentTranparency then
			table.insert(blacklistTable,ray.Instance)
		elseif mod == 3 then
			break
		end
		if typeof(mod) ~= "number" then
			break
		end
	until tick() == 0
	return ray
end

return module

How to use?

  1. require the module
  2. To raycast, you need to write
    SmartRaycast:RayCast(Origin,Direction,Prams,Mode)

if you leave mode empty, it will function as regular raycasting

3 Likes