Glitches with shooting into the void

Here is my code.

local magnitude = (gun.Nozzle.Position - mouseP).Magnitude 
	
local PointCFrame = CFrame.new(mouseOrigin.p, mouseP)
mouseP = PointCFrame.p + (PointCFrame.LookVector * 50)
	
local direction = (gun.Nozzle.Position - mouseP).Unit
local distance = 0.05 * magnitude
local p1 = mouseP + (direction * distance)
local p2 = mouseP - Vector3.new(0,-(0.2 * magnitude),0)
	
return p1, p2 , magnitude

mouseP is basically mouse.Hit.p
The problem: When I click into the void p2 ends up 2000 studs above where it’s supposed to be. Any solutions?

Check to see if Mouse.Target is not nil. If it’s not nil, proceed with the code.

Is this a local script? If not and you are using a remote event then in your local script where you are firing the mouse to the server above that line say

-- put this above the line you use to fire mouse.hit.position to the server if it's not a localscript
if mouse.Target == nil then return end

I don’t think you understand what I was trying to do but no matter because I figured it out. I’m not gonna explain how I did it though because it’s complicated (and probably very inefficient.)

I’m not sure what you meant so I kind of just guessed on what you were trying to do, the void shows up as nil so what I was trying to do was before mouse.Hit.Position updated it could not update since the void shows up as nil in mouse.Target and that line would prevent it from doing so. Thanks for the solution by the way.

1 Like