Anti wall-shooting system

I’ve been working on a ‘system’ to prevent wallshooting, this can be achieved by raycasting from the player’s head if wallshooting is detected.

I managed to get the detecting part done, however making it so it raycasts from the head is a bit more difficult.

local hit, pos, normal, material = workspace:FindPartOnRayWithIgnoreList(ray, ignore)
local distance = (origin - pos).magnitude

	local WallRay = Ray.new(object.Parent.Head.Position, (pos - object.Parent.Head.Position).unit * (distance - 10))
		local WallP, WallPositions = game.Workspace:FindPartOnRayWithWhitelist(WallRay, {workspace.NotisPart})
		
		if WallP and WallPositions then
			print("Wallshooting")
			Ray.new(object.Parent.Head.Position, (pos - object.Parent.Head.Position).unit * (distance - 10))
		end

Does Anyone know what I’m doing incorrectly? Any help is appreciated.

3 Likes

I found a fix to this by doing a raycast in the player arm, raycasting from the player hand to the part where the gun fires from, if there is a wall between them, it wouldn’t shoot (Don’t forget to blacklist character and gun in the raycast)

Assuming this line of code is correct then but I should instead raycast from the arm?

Ray.new(object.Parent.Head.Position, (pos - object.Parent.Head.Position).unit * (distance - 10))
1 Like

1. If the arm goes through the wall, then I think you should do 2 raycasts, one from HumanoidRootPart to RightHand, and another from RightHand to the fire part of the gun.

2. I think you should probably switch to roblox’s new raycasting here.

1 Like

I’m not sure if this would help you with the anti wall shooting but what I would do is firing a ray from either HumanoidRootPart or from the character head and then just create a bullet part as an visualiser from the gun itself. This way players wouldn’t be able to fire their gun through the walls.

Here’s the code of what I did.

local FirePoint = Gun.FirePoint.Position
local HumanoidRootPart = Player.Character.HumanoidRootPart
local Raycast = Ray.new(HumanoidRootPart.Position, (Mouse.Hit.p - HumanoidRootPart.Position).Unit * 50)
local Hit, Position = game.Workspace:FindPartOnRayWithIgnoreList(Raycast, {Player.Character}, false, true)
local Distance = (FirePoint - Position).magnitude
local Bullet = Instance.new("Part", workspace.CurrentCamera)
game:GetService("Debris"):AddItem(Bullet, .35)
Bullet.Name = "Bullet"
Bullet.Anchored = true
Bullet.CanCollide = false
Bullet.Size = Vector3.new(.08, .08, Distance)
Bullet.Transparency = 0
Bullet.BrickColor = BrickColor.new("Gold")
Bullet.Material = Enum.Material.Neon
Bullet.CFrame = CFrame.new(FirePoint, Position) * CFrame.new(0, 0, -Distance/2)
8 Likes