How to make a gun that bullet doesn't goes through walls?

Hello. I was working on my project then I saw that bullet goes through walls!
My gun is like this:
a bullet that tweens to the mouse.hit

I tried to use region3 to check what’s inside the bullet with a ignore list

The local script in the tool:
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local CanShoot = false
local CanKeepShoot = true
local CanReload = true
local Tool = script.Parent
local AmmoPack = 5
local Ammo = 10
local UIS = game:GetService(“UserInputService”)

local function Reload()
	if AmmoPack >= 0 then
		if CanReload == true then
			local animtype = "Reload"
			script.Parent.PlayAnim:FireServer(animtype)
			CanReload = false
			CanKeepShoot = false
			wait(1.5)
			Ammo = 10
			AmmoPack = AmmoPack - 1
			CanReload = true
			CanKeepShoot = true
		end
	end
end

UIS.InputBegan:Connect(function(keycode, ischatting)
	if ischatting == false then
		if keycode.KeyCode == Enum.KeyCode.R then
			Reload()
		end
	end
end)

Tool.Equipped:Connect(function()
	CanShoot = true
end)

Tool.Unequipped:Connect(function()
	CanShoot = false
end)

Mouse.Button1Up:Connect(function()
	if CanShoot == true then
		if CanKeepShoot == true then
			if Ammo >= 0 then
				Ammo = Ammo - 1
				local cframe = Mouse.Hit
				script.Parent.Shoot:FireServer(cframe)
				local animtype = "Shoot"
				script.Parent.PlayAnim:FireServer(animtype)
				CanKeepShoot = false
				wait(0.5)
				CanKeepShoot = true
			else
				Reload()
			end
		end
	end
end)

a script inside the tool that handles remote events:
local TweenService = game:GetService(“TweenService”)

local Tool = script.Parent

local Info = TweenInfo.new(

0.1,

Enum.EasingStyle.Linear,

Enum.EasingDirection.Out,

0,

false,

0

)

script.Parent.Shoot.OnServerEvent:Connect(function(player, cframe)

local Bullet = game.ReplicatedStorage.Bullet:Clone()

Bullet.Parent = game.Workspace

Bullet.CFrame = Tool.Handle.CFrame

--Bullet.CFrame = cframe

--Bullet.Position = Tool.Handle.Position

--Bullet.CFrame = Bullet.CFrame + Bullet.CFrame.LookVector * 2

Bullet.Owner.Value = player.Name

local newcframe = cframe + cframe.LookVector * 10

local Tween = TweenService:Create(Bullet, Info, {CFrame = cframe})

Tween:Play()

Tween.Completed:Connect(function()

Bullet:Destroy()

end)

end)

And the script inside the bullet:

local region = Region3.new(script.Parent.RegionPart1.Position, script.Parent.RegionPart2.Position)
local player = game.Workspace:FindFirstChild(script.Parent.Owner.Value)
local okparts = {
	script.Parent,
	script.Parent.RegionPart1,
	script.Parent.RegionPart2,
}
if player then
	for i,v in ipairs(player:GetChildren()) do
		table.insert(okparts, v)
	end
end
local partsinregion = game.Workspace:FindPartsInRegion3WithIgnoreList(region, okparts, 100)

while true do
	wait(0)
	region = Region3.new(script.Parent.RegionPart1.Position, script.Parent.RegionPart2.Position)
	local partsinregion = game.Workspace:FindPartsInRegion3WithIgnoreList(region, okparts, 100)
	if #partsinregion >= 1 then
		for i,v in ipairs(partsinregion) do
			if v.Parent:FindFirstChild("Humanoid") or v.Parent.Parent:FindFirstChild("Humanoid") then
				local humanoid = v.Parent:FindFirstChild("Humanoid") or v.Parent.Parent:FindFirstChild("Humanoid")
				humanoid:TakeDamage(15)
				if humanoid.Health <= 0 then
					local playernotcharacter = game.Players:FindFirstChild(script.Parent.Owner.Value)
					playernotcharacter.leaderstats.Cash.Value = playernotcharacter.leaderstats.Cash.Value + 60
				end
			end
			print(v.Name)
		end
		script.Parent:Destroy()
	end
	wait(0)
end

Now, what’s the problem?

The bullet goes through walls and the player can damage itself

Now how do I prevent these from happening?

1 Like

To prevent the bullet from going through walls, it’s recommended to use Raycasting. You can fire an invisible ray from the tip of the gun towards the requested target and see what block is being hit. You can also implement a Blacklist which will allow certain parts to be ignored.

To prevent the player from damaging themselves, you can run a check to see if the character that has been hit is owned by the player who fired the weapon.

2 Likes

How do I exactly get parts that are being hit?

AlvinBlox explained the use of Raycast for weapons very well. I still make my own confusions, so I’m just going to link his video to you. You’re especially interested at the part starting from 4:04.