To prevent people from shooting through walls by putting their weapons inside the wall and then firing, I was suggested the idea of using a raycast from the barrel to the head and in if a part is detected, the gun won't fire. This doesn't seem to be working for me when I put it in action - the raycast doesn't work as intended (not working on certain areas etc...).

	local firing = true
	local gunray = Ray.new(Gun.Barrel.Position, Head.Position)	
	
	local part, endpoint = workspace:FindPartOnRay(gunray, Character)
	if part then
		if part.Parent.name ~= Gun.Name then
			firing = false
		end
	end
	

All help is appreciated, thank you!

2 comments

0

Have you tried starting the ray from the start of the players arm, or just before the start?

0

local gunray = Ray.new(Character:FindFirstChild("Right Arm").CFrame.p, Gun.Main.CFrame.p) - Is this what you mean, as it still doesn't work and is finding parts that it shouldn't