How would i make the bullet not go threw the wall?

This is the local script

local function Pew()
animator = plr.Character:WaitForChild(“Humanoid”):WaitForChild(“Animator”)

if not loadedAnim[1] then
	loadedAnim[1] = animator:LoadAnimation(anim[1])
end

loadedAnim[1]:Play()
update_ammo()
FireEvent:FireServer(Mouse.Hit.p, tool.Handle, FirePart,"Pistol")
tool.Bullets.Value = tool.Bullets.Value - 1
LocalPlayer.Character.Humanoid.WalkSpeed = 16

end

local function Shoot()
if Cooldown == true then
return
end
if tool.Bullets.Value == 0 then
tool.Handle.ClipEmpty:Play()
return
end
Cooldown = true
LocalPlayer.Character.Humanoid.WalkSpeed = 10
Flash1.Enabled = true
Flash2.Enabled = true
Flash3.Enabled = true
Pew()
wait(CoolDownTime)
Flash1.Enabled = false
Flash2.Enabled = false
Flash3.Enabled = false
Cooldown = false
end

tool.Activated:Connect(function()
if ReloadDebounce == false then
Shoot()
end
end)

And this is the server Script where the bullets are created

PistolEvent.OnServerEvent:Connect(function(Player,Target,Handle,FirePart, gunType)

if gunType == "Pistol" then
	Handle.ShotSound:Play()

	local Beam = Instance.new("Part", workspace)
	Beam.BrickColor = BrickColor.new("White")
	Beam.FormFactor = "Custom"
	Beam.Transparency = 0.25
	Beam.Material = "Neon"
	Beam.Anchored = true
	Beam.CanCollide = false
	
	local Distance = (FirePart.Position - Target).magnitude
	Beam.Size = Vector3.new(0.1,0.1,Distance)
	Beam.CFrame = CFrame.new(FirePart.Position, Target)* CFrame.new(0,0, -Distance/2)

	game.Debris:AddItem(Beam, 0.1)

	local NewRay = RaycastParams.new()
	local RayDirection = (Target - FirePart.Position) * Range

	NewRay.FilterDescendantsInstances = {Player.Character}

	local Result = workspace:Raycast(FirePart.Position, RayDirection, NewRay)

	if Result then
		if Result.Instance then
			if Result.Instance.Parent:FindFirstChild("Humanoid") then
				Result.Instance.Parent.Humanoid.Health -= PistolDamage
			end
			if Result.Instance.Parent.Parent:FindFirstChild("Humanoid") then
				Result.Instance.Parent.Parent.Humanoid.Health -= PistolDamage
			end
		end
	end
end

I believe by default, the FilterType is set by Whitelist so you may need to change that to Blacklist instead?

if gunType == "Pistol" then
	Handle.ShotSound:Play()

	local Beam = Instance.new("Part", workspace)
	Beam.BrickColor = BrickColor.new("White")
	Beam.FormFactor = "Custom"
	Beam.Transparency = 0.25
	Beam.Material = "Neon"
	Beam.Anchored = true
	Beam.CanCollide = false
	
	local Distance = (FirePart.Position - Target).magnitude
	Beam.Size = Vector3.new(0.1,0.1,Distance)
	Beam.CFrame = CFrame.new(FirePart.Position, Target)* CFrame.new(0,0, -Distance/2)

	game.Debris:AddItem(Beam, 0.1)

	local NewRay = RaycastParams.new()
	local RayDirection = (Target - FirePart.Position) * Range

    NewRay.FilterType = Enum.RaycastFilterType.Blacklist
	NewRay.FilterDescendantsInstances = {Player.Character}

	local Result = workspace:Raycast(FirePart.Position, RayDirection, NewRay)

	if Result then
		if Result.Instance then
			if Result.Instance.Parent:FindFirstChild("Humanoid") then
				Result.Instance.Parent.Humanoid.Health -= PistolDamage
			end
			if Result.Instance.Parent.Parent:FindFirstChild("Humanoid") then
				Result.Instance.Parent.Parent.Humanoid.Health -= PistolDamage
			end
		end
	end
end

You’re resizing the part before raycasting.

You would wanna calculate the distance with RaycastResult.Position being the “Target” after you raycast (if the ray actually hits anything)

local Distance = (FirePart.Position - Result.Position).Magnitude

Beam.Size = Vector3.new(0.1,0.1,Distance)
Beam.CFrame = CFrame.new(FirePart.Position, Result.Position)* CFrame.new(0,0, -Distance/2)
2 Likes