My bullet velocity goes though walls

so i have a gun and the bullet uses used body velocity to make it hit
the part, but the thing is it goes though walls.

here a clip

and the code its in server script

gunevent.OnServerEvent:Connect(function(Player, Start, End)
---------Params
	local function SetRaycastParams()
		local params = RaycastParams.new()
		params.FilterType = Enum.RaycastFilterType.Exclude

		params.FilterDescendantsInstances = {char:GetDescendants(), tool}

		return params
	end

	local params = SetRaycastParams()
-----------Distance
	local function calcDir()
		local Orgian = Start
		local dir = End - Orgian

		local limit = (End - Start).Unit * 200

		return Start, limit, dir
	end
-------------Bullet
	local function rayVis(origin, respos, d, result)
		local HumanoidRootPart = char:FindFirstChild("HumanoidRootPart")
		local dist = (origin - respos).Magnitude
		
		local part = game:GetService("ReplicatedStorage"):WaitForChild("Bullet"):Clone()
		part.Parent = result.Instance
		part.Position = result.Position
		part.CFrame = CFrame.new(origin, respos)
		
		local bv = Instance.new("BodyVelocity")
		bv.Parent = part
		bv.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
		bv.Velocity = part.CFrame.LookVector * 70
		
		debris:AddItem(part, 2)
		

		
		

	end
-------------Bullet hole
	local function bullehole(result)
		local hole = replicates:WaitForChild("Bullehole", workspace.Ignore):Clone()
		hole.Anchored = true
		hole.CanCollide = false
		hole.Parent = result.Instance
		hole.Position = result.Position
		hole.CFrame = CFrame.new(hole.Position, hole.Position+ result.Normal)
		local wc = Instance.new("WeldConstraint", hole)
		wc.Part1 = result.Instance
		wait(2)
		game:GetService("TweenService"):Create(hole.Decal, TweenInfo.new(0.5), {Transparency = 1}):Play()

		task.delay(1, function()
			game:GetService("TweenService"):Create(hole, TweenInfo.new(2), {Size = Vector3.new(0.97, 0.886, 0.009)}):Play()
			game:GetService("TweenService"):Create(hole.Decal, TweenInfo.new(0.5), {Transparency = 1}):Play()
			game:GetService("Debris"):AddItem(hole, 1)	
		end)
		
	end
------------Raycast
	local function ray()
		local Orgian, dir = calcDir()

		local result = _Workspace:Raycast(Orgian, dir, params) or (Orgian + dir)
		
		
		if result then
			print(result)
			local hit = result.Instance
			local Humanoid = hit.Parent:FindFirstChild("Humanoid")
			local Head = hit.Parent:FindFirstChild("Head")
			
			if Humanoid then
				if hit.Name == "Head" then
					Humanoid.Health -= 50
				else
					Humanoid.Health -=10
				end
			end
		end
		
		if not result then return end
	
		rayVis(Orgian, result.Position, dir, result )
		bullehole(result)

		print("Bullet created")
	end
	ray()
	
end)```
1 Like

This seems to be the problem. Change it to

debris:AddItem(part, (result.Position-part.Position).Magnitude/bv.Velocity.Magnitude)

See if it worked.

4 Likes

wow that fixed it idk how you did it but wow

1 Like

It measures the distance between the intersection and the firing point, and then divides that by bullet speed so that it doesn’t get deleted too early (before it hits the point) or too late (going through the wall).

It’s pretty much just physics:
distance / speed = time
(intersection - firing point) / (bullet speed) = time

Example:
Bullet travels at 2 studs a second, gun was shot from 5 studs north, bullet landed at 10 studs north

distance = 10 - 5
distance = 5

speed = 2

time = distance / speed
time = 5 / 2
time = 2.5

Bullet took 2.5 seconds before reaching target.

2 Likes

wow that makes a lot of sense ty

1 Like

No problem. If you have any questions about my explanation feel free to ask.

1 Like

wow okay sir i will ask you for sure

okay wait there a bug. when you go up to close to it it doesrnt show the bullet. or makes a small bullet. is this a distance problem and nota debis problem?

Are you using these animations on the client?

yes i am. is that a reason why the bullets are being broken?