Raycast Refusing to cast in the desired direction (Gun System)

  1. What do you want to achieve?

So I’ve been perfecting a fps system with a few guns in a test game to transfer the code and assets over to the offical game I’m developing.

Transferred over everything but for some reason the raycast system is failing to work with one of my guns.

  1. What is the issue?

The gun worked perfectly fine perviously in the test game (I did rebuild the viewmodel but with only a few adjustments) However when firing this gun spacifically, the Raycast Instance comes back as BasePlate not matter what direction I fire it.
Raycast Issue

Here are some screenshots of my outliner and my fire function for the gun. I’ll Put a copy of my raycast code as well:

local GunEvent = RS.RemoteEvents:FindFirstChild("GunEvent")
local remoteEvent = RS.RemoteEvents:FindFirstChild("ShotEvents")

function fireBullet(Start: Vector3, End: Vector3, Orientation: Vector3)
	findBullet()
	Bullet.Position = Start
	Bullet.Orientation = Orientation
	local goal = {}
	goal.Position = End
	local tweenInfo = TweenInfo.new(speed, Enum.EasingStyle.Linear)
	local tween = TweenService:Create(Bullet, tweenInfo, goal)
	
	RunService.Heartbeat:Wait()
	
	tween:Play()
end

GunEvent.OnServerEvent:Connect(function(player, item)
	for i, v in pairs(game.ReplicatedStorage.Guns.Modules:GetChildren()) do
		if v.Name == item then
			Module = require(v)
			print("Module is loaded")
			Min_Spread = Module.Min_Spread
			Max_Spread = Module.Max_Spread
			dropOff = Module.DropOff
			SpBullet = Module.IsSpecial
			crit = Module.Crit
			Chance = Module.Chance
			MinDmg = Module.MinDmg
			MaxDmg = Module.MaxDmg
		end
	end
end)

remoteEvent.OnServerEvent:Connect(function(player, gunPos, gunOr, mosPos)
	local startPoint = nil
	local endPoint = nil
	local Heading = nil
	
	local direction = (mosPos - gunPos).Unit
	local directionCF = CFrame.new(Vector3.new(), direction)
	local newDirection = (directionCF * CFrame.fromOrientation(0, 0, RNG:NextNumber(0, TAU)) * CFrame.fromOrientation(math.rad(RNG:NextNumber(Min_Spread, Max_Spread)), 0, 0)).LookVector
	 
	RayCastParams.FilterDescendantsInstances = {player}
	RayCastParams.FilterType = Enum.RaycastFilterType.Exclude
	RayCastParams.IgnoreWater = true
	
	local raycastResult = workspace:Raycast(gunPos, newDirection * dropOff, RayCastParams)
	
	if raycastResult then
		if raycastResult.Distance <= dropOff then
			startPoint = gunPos
			endPoint = raycastResult.Position
			Heading = gunOr
		else
			startPoint = gunPos
			endPoint = gunPos + (newDirection * dropOff)
			Heading = gunOr
		end
	else
		startPoint = gunPos
		endPoint = gunPos + (newDirection * dropOff)
		Heading = gunOr
	end
	
	print(raycastResult.Instance)
	fireBullet(startPoint, endPoint, Heading)
	wait(speed)
	CreateHole(raycastResult)
  1. What solutions have you tried so far?

I’ve tried figuring out what is the root cause but I can only think that is something in the gun or fire function I hve in the screenshot I sent above as all of my other guns work with this system.

If anyone can see any anomalies anywhere your help would be much apriciated :slight_smile:

Print everything: mosPos, gunPos, newDirection, raycastResult.Position, etc.

Something is probably going to stick out as being wrong.

Also, this:
RayCastParams.FilterDescendantsInstances = {player}

should probably be this:
RayCastParams.FilterDescendantsInstances = {player.Character}

You’re trying to make it so the player can’t shoot their own avatar, right? The player parameter in a RemoteEvent is the actual Player instance, not their character model. I can’t see where you set up the RaycastParams instance, but you have to make sure also that you’re not re-using the same one and adding every player’s character to it when they shoot, otherwise you’ll end up with a RaycastParams that ignores everyone and no one will be able to shoot anyone else who has fired a shot.

1 Like

Raycasts can be a bit tricky to debug, but they don’t have to be!
I would recommend you visualize your raycast by creating a part representing the origin, end point, and the ray itself. This way you can get a better idea of where your code is wrong.

Having read over your code, I don’t believe there’s any issue with the way you are handling bullet spread, nor with how you are raycasting, so it is likely an issue with some of the parameters passed to the server.