Raycast from camera cframe occasionally going in a random direction

So I am working on a gun system for a game and for now im just creating parts to represent the bullets however, I noticed occasionally the rays will go in a complete random direction

I’m also using a mix of my old code from a system from a few years ago so please excuse any mess. This is whats doing the actual like raycasting

local module = function(camera : CFrame, weapon, plr : Player)
	local modules = game:GetService("ReplicatedStorage").WSAv3.Modules
	local remotes = game:GetService("ReplicatedStorage").WSAv3.Remotes
	
	local functions = require(modules.Functions)
	local damageHumanoid = require(modules.DamagePlayer)
	
	local config = game:GetService("ReplicatedStorage").WSAv3.WeaponConfigs:FindFirstChild(weapon.."-CONFIG")
	if config then
		local config = require(config)
		local direction = camera.LookVector
		local ray1 = Ray.new(camera.Position, direction*config.MaxDist)
		local ignoreList = require(script.Parent.RayIgnoreList)
		
		for _, child in plr.Character:GetDescendants() do
			table.insert(ignoreList, child)
		end
		
		local result = workspace:FindPartOnRayWithIgnoreList(ray1, ignoreList, true, true)
		print(result)
		local intersection = result and result.Position or camera.Position + direction*config.MaxDist
		local distance = (camera.Position - intersection).Magnitude
		local rounded = math.floor(distance + 0.5)
		local bullet = Instance.new("Part")
		bullet.Size = Vector3.new(0.1, 0.1, distance)
		bullet.CFrame = CFrame.new(camera.Position, intersection)*CFrame.new(0, 0, -distance/2)
		bullet.Parent = workspace.MouseFilter
		bullet.Anchored = true
		bullet.CanCollide = false

		bullet.BrickColor = BrickColor.Yellow()
		--[[
		
		]]
		wait(2)
		bullet:Destroy()
	end
end
return module

Looks like it could be latency on the server causing the ray to fire but lagged from the clients position. Is the script on the server or client?

Its on the server but seems to still occur when the camera is still

Yea I just moved it to the client and it still has the same issue

Hi, try to use workspace:Raycast instead of workspace:FindPartOnRayWithIgnoreList

Specific methods like workspace:FindPartOnRayWithIgnoreList have caused me issues in the past while working with rays.

Also make sure your rayDirection is not a vector of the position where you wish the ray to end, but a lookVector. Here’s an example of how I raycast in my scripts:

-- Raycast parameters, basically your ray settings.
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {} -- Here is your ignore list!
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
raycastParams.IgnoreWater = true



local rayOrigin = Vector3.new(0,0,0) -- the start of your ray
local rayTarget = Vector3.new(0,-5,0) -- the end of your ray

-- tada! we got the ray direction
local rayDirection = CFrame.new(rayOrigin,rayTarget).LookVector

-- this is the part that your ray hit
local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)

if raycastResult then
    print("Raycast result found!", raycastResult.Name)
    -- your code here...
end

Also, you’re not checking whether or not you have a result while raycasting! Please add an if statement to see if result is a thing.

You’re welcome : )