Bullets continuing through objects

I’ve been working on the same project for a week and I’ve bumped into another problem. This seems like the last one, but it seemed to be quite a big issue.

The issue is that the bullets will go through objects despite the raycast stopping at the said object. I’ve tried switching the math around, but it would give me a part that is far away from the barrel of the gun or just not appear in sight at all.

https://gyazo.com/05b1580ff9d9892f35ed415b8b4e79db

local dir = (mouse - pos.CFrame.p).Unit *300
local mid = pos.CFrame.p + dir/2 
local bullet = Instance.new("Part")
bullet.Parent = workspace
bullet.CFrame = CFrame.new(mid, pos.CFrame.p)
bullet.Size = Vector3.new(.3,.3,dir.magnitude)

All of these are wrapped in the same function, and I’ve narrowed it down to this spot.

just check if the bullet hits then destroy or shoot a raycast checking if it hits something.

you want to get the rays length. An easy way to do this is to do something like bullet.Size = Vector3.new(.3,.3,(gun.Position - ray.Hit.Position).Magnitude)

How exactly do I get the ray size?

i literally just told you how… (gun.Position - ray.Hit.Position).Magnitude