[SOLVED] Gun raycast not registering at random intervals

My gun system’s raycast does not register at random intervals while shooting, here’s some in-depth information about the raycast calculations:

local Origin = MuzzlePointPosition
local Direction = (MousePosition - Origin).Unit * Attributes.BulletDistance

local FinalPosition = Direction --[[CFrame.Angles(math.rad(math.random(-0, 0)), math.rad(math.random(-0, 0)), math.rad(math.random(-0, 0))) *]] 
local HitPart = game.Workspace:Raycast(Origin, FinalPosition, Raycastparams)

The muzzle point position is the player’s camera.

Camera.CFrame.Position

This raycast is on the client, and it’s the exact same on the server. The server does a sanity check for the raycast every time.

This is the calculation I use for the sanity check on the server:

(HitPart.Position - ClientPosition).Magnitude < 20

Here’s a video of this issue happening

External Media

Further questions about the gun system could be addressed in replies and I can try my best to provide as much information as possible

1 Like

Well with your adjustment here is your new Client-side with Camera.CFrame.Position

local Origin = Camera.CFrame.Position
local Direction = (MousePosition - Origin).Unit * Attributes.BulletDistance
local HitPart = game.Workspace:Raycast(Origin, Direction, Raycastparams)

Server-side sanity check:

if HitPart and (HitPart.Position - ClientPosition).Magnitude < 50 then
end
2 Likes

If you are facing some issues with a custom gun raycast system, you may want to use something like FastCast
The post can be found over here, should be pretty easy to use.

As far as the code, @PowFPS1’s reply should work aswell.

3 Likes

This still did not seem to fix the issue, I already had a check to see if the HitPart/HitPart.Instance exists or not

Though I found the exact problem the gun system has:

On the client, the HitPart exists, passes through the check, and sends the data to the server. This is where the issue comes, because when data is sent to the server, for some reason… it comes out as nil?? Even though again, it passes through the client’s check??


EDIT: This is more common when the player moves around and shoots

1 Like

Replacing Mouse.Hit.p with a custom mouse raycast seemed to solve the issue I had with this

local DefaultRaycastParams = RaycastParams.new()
DefaultRaycastParams.FilterType = Enum.RaycastFilterType.Exclude
DefaultRaycastParams.FilterDescendantsInstances = {Player.Character, Tool, game.Workspace.World.Temp}

local function MouseRaycast(raycastParams, distance): any
	local MOUSE_POSITION = game:GetService("UserInputService"):GetMouseLocation()
	local ViewportMouseRay = game.Workspace.CurrentCamera:ViewportPointToRay(MOUSE_POSITION.X, MOUSE_POSITION.Y)
	return workspace:Raycast(ViewportMouseRay.Origin, ViewportMouseRay.Direction * (Attributes.BulletDistance), raycastParams)
end

Full raycast function with the new mouse raycast:

local Raycastparams = RaycastParams.new()
Raycastparams.FilterType = Enum.RaycastFilterType.Exclude
Raycastparams.FilterDescendantsInstances = {Player.Character, Tool, game.Workspace.World.Temp}
Raycastparams.IgnoreWater = true

local MouseRaycastReturned = MouseRaycast(DefaultRaycastParams)
				
local Origin = MuzzlePointPosition
local Direction = (MouseRaycastReturned.Position - Origin).Unit * Attributes.BulletDistance

local FinalPosition = Direction --[[CFrame.Angles(math.rad(math.random(-0, 0)), math.rad(math.random(-0, 0)), math.rad(math.random(-0, 0))) *]] 
local HitPart = game.Workspace:Raycast(Origin, FinalPosition, Raycastparams)

if HitPart and HitPart.Instance then

	RemoteEvent:FireServer("HitRegister", MouseRaycastReturned.Position, Camera.CFrame.Position, HitPart.Position, HitPart.Normal, HitPart.Instance)

else

end

Thanks to @SnowLeaf_YT for having a solution for this issue

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.