Raycast not using ignore list

In the while loop, when it casts the ray, it just hits the viewmodel which annoys me a lot.

local ig = {game.Workspace.CurrentCamera,Character} --the list with the ignores in it
	for i = 1, gunfolder.Bullets.Value do
		if gunfolder.Bullets.Value == 1 then
			print(i)
			local dir
			dir = CFrame.new(p1,p2)*CFrame.Angles(math.rad(math.random(gunfolder.Spread.Value/10,gunfolder.Spread.Value/10)),math.rad(math.random(gunfolder.Spread.Value/10,gunfolder.Spread.Value/10)),0)
			local size = 1 
			local increment = 1 -- how big your backwards ray will increase every iteration
			local penetrationDepth = 5 --how far your ray is allowed to go through the wall

			local origin = p1 -- the origin of your original ray
			local hitpos = p2

			local hit, pos

			while size < penetrationDepth do --courtesy of rek_kie https://devforum.roblox.com/t/bullet-wallbanging-is-not-functioning-properly/1031719
				local rey = Ray.new(hitpos, p1,dir.LookVector* size) 
				local hit, position,normal = workspace:FindPartOnRayWithIgnoreList(rey, ig, false, true)
				print(hit)
				if not hit then 
					size += increment 
				else
					if hit.Parent:IsA("Accessory") then
						table.insert(ig,hit) -- adds to the ignore list
						return Raycast(p1,p2,dist) -- do the raycast again
					end
					break
				end 

				local ray = Ray.new(p1,dir.LookVector*dist)
				local clone = game.ReplicatedStorage.ray:Clone()
				clone.Parent = game.Workspace
				clone.p1.WorldPosition = origin
				clone.p2.WorldPosition = hitpos
				game.Debris:AddItem(clone,2)

				local part, position,normal = workspace:FindPartOnRayWithIgnoreList(ray, ig, false, true)
				if part then
					if part.Parent:IsA("Accessory") then
						table.insert(ig,part) -- adds to the ignore list
						return Raycast(p1,p2,dist) -- do the raycast again
					else
						print(part.Name,part.Parent.Name)
						game.ReplicatedStorage.cast:FireServer(part,gunfolder)
						if config.KillAndHitSounds then
							if part.Parent then
								if part.Parent:FindFirstChild("Humanoid") then
									game.SoundService:PlayLocalSound(game.ReplicatedStorage.Sounds.Hit)
								end
							end
						end
						local newcastpos = p2
						local ray = Ray.new(newcastpos,dir.LookVector*dist)
						local part, position,normal = workspace:FindPartOnRayWithIgnoreList(ray, ig, false, true)
						if part then
							local clone = game.ReplicatedStorage.ray:Clone()
							clone.Parent = game.Workspace
							clone.p1.WorldPosition = origin
							clone.p2.WorldPosition = position
							game.Debris:AddItem(clone,2)
							if part.Parent:IsA("Accessory") then
								table.insert(ig,part) -- adds to the ignore list
								return Raycast(p1,p2,dist) -- do the raycast again
							else
								print(part.Name,part.Parent.Name)
								newcastpos = part.Position
								game.ReplicatedStorage.cast:FireServer(part,gunfolder,true)
							end
						end
						return part,position,normal
					end
				end
			end
		end
	end
end

Use WorldRoot:Raycast()
It’s simpler and gives you better results.

Does that solve the fact that it hits the viewmodel?

I’m not sure, but FindPartOnRay…() Is deprecated so it may not work well, therefore hitting the viewmodel

But it’s worked before without hitting the viewmodel.

Hmm. Try putting the actual view model into the ignore list, not the camera itself.

Okay! I’ll try it and see if it works.

That didn’t work. I’m so confused as to why it wont work.

Oh. Is it a server script? The ViewModel is probably on the client, right?

No, It’s a local script just so that there’s no delay to shooting.

Try casting the ray a few studs in front of the viewmodel, so there’s no way it could hit it?

I guess but I don’t want players shooting through walls. (even if that’s what the code’s for)

And also it’s a vector3 value so making it go just infront of the camera might be a bit hard.

What about getting the distance from mouse.Hit to Camera.CFrame and if it’s lower than the distance betwen the ray starting point to the camera then just shoot at mouse.Hit without raycasting because there is 100% a part

The wallbang code already does that but it hits the dummy twice.

Maybe try checking if the code already shot without a raycast and then raycasting if not.
like:

if mousehitMagnitude < 3 then
ShotWithoutRay = true
Shoot(mouse.Hit.Position)
end
if not ShotWithoutRay then
local hit = CastRay()
Shoot(hit.Position)
end

Actually, instead of 2 if statements, make an else statement. My bad.


What it’s meant to look like when aiming

image
What it actually looks like when aiming

Visualised with a triangle by the way.

I have no idea what could be causing the issue or how to solve it.

Is Character a reference to the ViewModel? I don’t see it used anywhere in the provided script.