Raycasting Inconsistency issues!

Hello! I’m currently experiencing an issue, where my raycast script (local) only sometimes works, and does not react to the range parameter at all! This is probably an oversight i’ve made, but i’ve become very annoyed by it. I’d appreciate any help!

Local Script:

--> Services
local Replicated_Storage_Service = game:GetService("ReplicatedStorage")
local Context_Action_Service = game:GetService("ContextActionService")
local Player_Service = game:GetService("Players")
local User_Input_Service = game:GetService("UserInputService")

--> References
local Modules_Folder = Replicated_Storage_Service:WaitForChild("Modules")

--> Player
local Player = Player_Service.LocalPlayer
local Character = Player.Character
local Mouse = Player:GetMouse()

--> Modules
local CF = require(Modules_Folder.Custom_Functions)

--> Values
local Action_Name_1 = "Attack"
local Range = 1
local Debounce = false

--> Main Functions
local function Attack()
	if Debounce == false then
		Debounce = true
		print("Fired")
		local Hit_Part, Position = CF.Raycast(Character.HumanoidRootPart.Position, Mouse.Hit.p, Range, Character, true)
		print(Hit_Part)
		
		task.wait(0.4)
		Debounce = false
	end
end

Context_Action_Service:BindAction(Action_Name_1, Attack, true, Enum.UserInputType.MouseButton1)

It isn’t complete, as i was just trying to get the raycast working! Here’s the module it calls upon:

function CF.Raycast(Start, Direction, Range, Ignore, Debug)
	
	local Raycast_Parameters = RaycastParams.new(); Raycast_Parameters.FilterDescendantsInstances = {Ignore}; Raycast_Parameters.FilterType = Enum.RaycastFilterType.Blacklist
	local Raycast_Result = workspace:Raycast(Start, (Direction - Start) * Range, Raycast_Parameters)
	
	if Raycast_Result then
		
		if Debug == true then
			local distance = (Start - Raycast_Result.Position).Magnitude
			local p = Instance.new("Part")
			p.Anchored = true
			p.CanCollide = false
			p.Size = Vector3.new(0.1, 0.1, distance)
			p.Color = Color3.new(1, 0, 0)
			p.Transparency = 0.7
			p.CFrame = CFrame.lookAt(Start, Raycast_Result.Position)*CFrame.new(0, 0, -distance/2)
			p.Parent = game.Workspace
			
		end
		
		print("Returned")
		return Raycast_Result.Instance, Raycast_Result.Position, Raycast_Result.Normal
	else
		print("No Result!")
	end
end

I’ve found a raycast visualiser, which i used to try to help me diagnose it! Now here’s a video of the issue: Raycasting Issues - Only works sometimes!

As you can see from the video above (i’ve uploaded it to streamable.com as it was too large), you can see the majority of the time, it returns nil as shown in the output! Does anyone have any help on this? I’d appreciate it a ton!

I tested the code in an empty workspace and I may have found the reason.

As you can see in the image below, I fired RayCasts while walking in a straight line. This resulted in more consistent results because the new RayCast was not intersecting the other RayCasts.

When I did a circular cast, it came out with less RayCasts because the starting position of the RayCasts shared the same position. Therefore, it was intersecting with the other RayCasts.

But when I disabled debug mode, it was sharing the same results as debug mode. Do you think perhaps it is because the end position needs to be a distance farther than the mouse position, for example, 2-3 units past the position of the mouse and its’ lookvector?

https://gyazo.com/b129642aa0304fdf81b746794f22d6f2.mp4

Hello! Thank you for your response! I don’t see how the start position would affect how the raycast works, as it essentially disappear as it’s been made. The main body of my raycast code was grabbed from my gun system, which works fine! The end position would have to be defined by the range, as this is a melee attack

Hello! Just to let everyone know, i’ve sort of got it working! It is now consistent, but still doesn’t oblige to the range, however, i’ll just do a distance check to combat that!

Module Code

function CF.Raycast(Start, Direction, Range, Ignore, Debug)
	
	local Raycast_Parameters = RaycastParams.new(); Raycast_Parameters.FilterDescendantsInstances = {Ignore}; Raycast_Parameters.FilterType = Enum.RaycastFilterType.Blacklist
	local Raycast_Result = workspace:Raycast(Start.Position, (Direction - Start.Position) * Range, Raycast_Parameters)
	
	if Raycast_Result then
		
		if Debug == true then
			local distance = (Start.Position - Raycast_Result.Position).Magnitude
			local p = Instance.new("Part")
			p.Name = "Debug"
			p.Parent = game.Workspace.Debug
			p.Anchored = true
			p.CanCollide = false
			p.Size = Vector3.new(0.1, 0.1, distance)
			p.Color = Color3.new(1, 0, 0)
			p.Transparency = 0.7
			p.CFrame = CFrame.lookAt(Start.Position, Raycast_Result.Position)*CFrame.new(0, 0, -distance/2)
			
		end
		
		print("Returned")
		return Raycast_Result.Instance, Raycast_Result.Position, Raycast_Result.Normal
	else
		print("No Result!")
	end
end

Local Script Changes:

local Hit_Part, Position = CF.Raycast(Character.HumanoidRootPart, Mouse.Hit.p, Range, Character and game.Workspace.Debug:GetChildren(), true)

If anyone knows how to make the range work, please let me know! I’ve set it to 20 and it seems to be working fine. Good luck to any having the same issue!