Raycast doesn't register

hello!! lately i’ve been working on a gun system just for fun but i’ve been getting stuck with raycasting.

What is the problem?

the fired raycast sometimes doesn’t register and returns nil. this happens when i generally shoot.
for example when the raycast hits the ground or a npc it doesn’t register.
also, at a certain position the raycast ignores parts, going straight through them. this also returns nil even though, visually, the bullet goes straight through and can be seen clearly colliding.

here’s some screenshots:
screenshot4
screenshot3
screenshot2
screenshot1

i’ve searched through the dev forum but i still can’t figure this out.
any help would be greatly appreciated!!

here’s some code:

Client Side:

local function raycastInfo(inputPosition, maxDistance)
	local ray = cam:ScreenPointToRay(inputPosition.X, inputPosition.Y) 
	ray = Ray.new(ray.Origin, ray.Direction * maxDistance)
	
	local hit = workspace:Raycast(ray.Origin, ray.Direction, params)
	if hit then
		local calcDir = (hit.Position - firePart.Position) -- no need to turn into .Unit form

		local finalRay = Ray.new(firePart.Position, calcDir)
		return finalRay
	else
		return nil
	end
end

uis.InputBegan:Connect(function(input, gp)
	if gp then return end
	if not equipped then return end

	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		local info = raycastInfo(input.Position, 100)
		if info then
			shootEvent:FireServer(mouse.Hit.Position, 100, info.Origin, info.Direction)	
		end
	end
end)

Server Side:

shootEvent.OnServerEvent:Connect(function(player, mousePos : Vector3, dist, origin, dir)
	if not equipped then return end

	print("shoot event")
	--
	fireSound:Play()
	--
	local params = RaycastParams.new()
	params.FilterType = Enum.RaycastFilterType.Exclude
	params.FilterDescendantsInstances = {player.Character}

	local result = workspace:Raycast(origin, dir, params)

	print("pass debounce")
	--		
	if result then
		--print(result, result.Instance)
		--
		local origin2 = firePart.Position
		local target = result.Position
		local length = (target - origin2).Magnitude

		local part, ball = makePart()
		part.Parent = player.Character
		part.CFrame = CFrame.lookAt(origin2, target) * CFrame.new(0, 0, -length / 2)
		part.Size = Vector3.new(part.Size.X, part.Size.Y, length)

spawn(function()
				ball.Parent = player.Character
				ball.Position = result.Position --result.Position

				for i = 1, 0, -0.1 do
					ball.Transparency = i
					--
					task.wait(0.025)
				end
			end)

			game.Debris:AddItem(part, 0.2)
			game.Debris:AddItem(ball, 0.2)
	end
end)
1 Like

Can you add a print after the “if info then”?

if info then 
print(info.Origin, info.Direction)
1 Like

sure
here’s a video and the output (video.mp4 - Google Drive)

  19:12:30.000  317.54779052734375, 50.53371810913086, 8.797077178955078 -4.821868896484375, -4.3336639404296875, -1.4769020080566406  -  Client - client:56
  19:12:30.044  shoot event  -  Server - server:38
  19:12:30.046  pass debounce  -  Server - server:51
  19:12:30.046  no result  -  Server - server:89
  19:12:30.046  creating part...  -  Server - server:18
  19:12:32.286  319.8128356933594, 50.53371810913086, 18.750991821289062 -5.86572265625, -4.3336639404296875, -1.0813674926757812  -  Client - client:56
  19:12:32.343  shoot event  -  Server - server:38
  19:12:32.344  pass debounce  -  Server - server:51
  19:12:32.344  creating part...  -  Server - server:18
  19:12:34.942  319.8340148925781, 50.53372573852539, 31.948122024536133 -6.25341796875, -4.133663177490234, -0.8705425262451172  -  Client - client:56
  19:12:34.981  shoot event  -  Server - server:38
  19:12:34.981  pass debounce  -  Server - server:51
  19:12:34.981  no result  -  Server - server:89
  19:12:34.981  creating part...  -  Server - server:18
  19:12:36.664  320.74713134765625, 50.73372268676758, 43.578800201416016 -6.353302001953125, -4.333660125732422, -0.5558090209960938  -  Client - client:56
  19:12:36.711  shoot event  -  Server - server:38
  19:12:36.712  pass debounce  -  Server - server:51
  19:12:36.712  no result  -  Server - server:89
  19:12:36.712  creating part...  -  Server - server:18
  19:12:38.391  319.786376953125, 50.73373031616211, 49.63200378417969 -10.0546875, -4.333667755126953, 0.8913040161132812  -  Client - client:56
  19:12:38.461  shoot event  -  Server - server:38
  19:12:38.462  pass debounce  -  Server - server:51
  19:12:38.462  no result  -  Server - server:89
  19:12:38.463  creating part...  -  Server - server:18