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

Hello. I made a working gun/pistol/Glock that shoots but for some reason, It doesn’t damage and it goes through walls

here is a localscript in my tool/gun

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)
			else
				Reload()
			end
		end
	end
end)

and the script

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.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)

How do i make it that the bullet doesnt goes through walls and damage players?

You could get the bullet and get the location every 2 seconds or something then make a ray of it’s last location and detect what the ray hit

2 Likes

Yeah, your advice would be better. Its much more simple and can give versatility to him/she to make different kinds of bullets that way.