How to make a gun system that stops shooting if the target goes behind a wall

So, im making a gun system like Isle game, and it works like this: when u click on a enemy u start aiming at it, then after a certain amount of seconds if the enemy is still in the player’s range it shoots, but idk how to make that part which stops shooting if the enemy goes behind a wall.

Could anyone help me?

The Script


local tool = script.Parent
local remote = tool:WaitForChild("OnShoot")
local shoot_part = tool:WaitForChild("FirePart")
local Config = tool.Config
local Damage = Config.Damage.Value
local SFX = tool:WaitForChild("Handle")
local Particle1 = shoot_part:WaitForChild("Muzzle")

local Shooting = false

remote.OnServerEvent:Connect(function(player, target)
	if not Shooting and Config.Ammo.Value > 0 and (player.Character.HumanoidRootPart.Position-target.Position).magnitude <= 100 then
		Config.Ammo.Value -= 1
		Shooting = true
		
		tool.Aim:FireClient(player)
		wait(2)
		Particle1.Enabled = true
		tool.Shoot:FireClient(player)
		target.Parent.ZombieNoid:TakeDamage(25)
		wait(0.2)
		Particle1.Enabled = false
		wait(3)
		Shooting = false
	end
end)
2 Likes

Send a raycast from the shooter to the enemy before shooting each bullet, if the ray doesn’t hit the enemy player then don’t shoot

1 Like

But then how i would make that? sorry i suck with raycast

local origin = shooter.HumanoidRootPart.Position
local direction = enemy.HumanoidRootPart.Position - shooter.HumanoidRootPart.Position

local rayResult = workspace:Raycast(origin, direction)

if rayResult.Instance.Parent == enemy then
    -- shoot
else
    -- don't shoot
end

sorry for replying late

this worked, thx!

30charact3rs