I am trying to make a gun system. In the past, I was creating parts with Instance.new() and using velocity to fire them in the direction of the player's mouse position, however this proved to be too laggy for the game I'm working on and so I decided to switch to using raycasting instead (I noticed a lot of people also use raycasting for making guns too). After experimenting a little bit, I got the gun to damage humanoids (I have been testing on dummies) however when I aim at the dummy sometimes the workspace:FindPartOnRay() returns nil and sometimes it returns the body part I was aiming at. Note: I kept my cursor still the whole time and I have not added any bullet spread to the gun so my aim was precise the whole time.

local ignoreList = {tool,plr.Character}
local direction = mousePos - handle.Flash.Position
local ray = Ray.new(handle.Flash.Position, direction)

local rayHit = workspace:FindPartOnRayWithIgnoreList(ray, ignoreList)
print(rayHit)
if rayHit then
	local model = rayHit:FindFirstAncestorOfClass("Model")
	local human = model:FindFirstChild("Humanoid")
	if model and human then
		human:TakeDamage(dmg)
	end
end