Raycast not detecting anything for some reason

I’m trying to make a system where an enemy launches a projectile at you that damages you.
The launching part works perfectly fine, however the raycast it fires never detects any instances, let alone players.

This is the script that I am using:

local closestPlayer = findClosestPlayer(sock)
			if closestPlayer then
				local roach = game.ReplicatedStorage.Roach:Clone() -- roach is the projectile
				roach.Parent = sock -- sock is the enemy
				roach.Position = sock.PrimaryPart.Position
				roach.CFrame = CFrame.lookAt(roach.Position, closestPlayer.Character.Head.Position)
				roach.CanCollide = false

				game:GetService("Debris"):AddItem(roach, 10)
				
				local params = RaycastParams.new()
				params:AddToFilter(roach)
				params:AddToFilter(sock)
				params:AddToFilter(workspace.Socks)
				for i, v in pairs(workspace:GetDescendants()) do if v:IsA("Accessory") then params:AddToFilter(v) end end
				params.FilterType = Enum.RaycastFilterType.Exclude
				
				delay(0, function()
					while roach do
						task.wait()
						roach.CFrame += roach.CFrame.LookVector * 1
					end
				end)
			
				local hit = workspace:Raycast(roach.Position, roach.CFrame.LookVector * 5, params)
				if hit then print(hit.Instance) end -- never prints anything
				if hit and game.Players:GetPlayerFromCharacter(hit.Instance.Parent) then
					print("hit")
					closestPlayer.Character.Humanoid:TakeDamage(3 + (game.ReplicatedStorage.Room.Value / 400))					
					roach:Destroy()
				end
			end

What essentially happens is that the projectile passes right through the player, as well as any other part.

Honestly I don’t know why this is the case, as the params seem to be in order.

Any help is appreciated. Many Thanks.

2 Likes

Try doing spherecast, also let me tell you, i have a grapple hook system and raycastparams dont work for me, so i decided to use if statements if that doesnt work, try using normal rays like i had t. It should work.

While this may not be right, ill guess why it aint working.
You’re adding a lot of things to the params, including accessories, what if the player’s characters has them?
And for your direction, what if its too short?
What I would do is, instead of excluding a lot of things in the params, i would just include the player’s character. And I would buff the direction as well
Final Code:

local closestPlayer = findClosestPlayer(sock)
			if closestPlayer then
				local roach = game.ReplicatedStorage.Roach:Clone() -- roach is the projectile
				roach.Parent = sock -- sock is the enemy
				roach.Position = sock.PrimaryPart.Position
				roach.CFrame = CFrame.lookAt(roach.Position, closestPlayer.Character.Head.Position)
				roach.CanCollide = false

				game:GetService("Debris"):AddItem(roach, 10)
				
				local params = RaycastParams.new()
				params.FilterDescendantsInstances = {closestPlayer.Character}
				params.FilterType = Enum.RaycastFilterType.Include
				
				delay(0, function()
					while roach do
						task.wait()
						roach.CFrame += roach.CFrame.LookVector * 1
					end
				end)
			
				local hit = workspace:Raycast(roach.Position, roach.CFrame.LookVector * 100, params)
				if hit then print(hit.Instance) end -- never prints anything
				if hit and game.Players:GetPlayerFromCharacter(hit.Instance.Parent) then
					print("hit")
					closestPlayer.Character.Humanoid:TakeDamage(3 + (game.ReplicatedStorage.Room.Value / 400))					
					roach:Destroy()
				end
			end