Raycast Projectile coming from world origin and not gun?

I recently I’ve scripted some guns for my game and the projectile that gets created sometimes doesn’t come from the shootpart (part at the end of the barrel) of my gun and instead from the worlds origin (0,0,0). Any reason that this is happening?

I’ve provided snippets of code below to help.

--Arguments passed through remote (Client above, server below)
FireWeaponRemote:FireServer(script.Parent.Name, MousePosition, script.Parent.Handle.ShootPart.Position)
FireWeaponRemote.OnServerEvent:Connect(function(Player, Weapon, MousePosition, ShootPartPosition)

--Creating Ray
local ProjectileBlacklist = Player.Character
local ProjectileDistance = (MousePosition - ShootPartPosition)
local Projectile = Ray.new(ShootPartPosition, (ProjectileDistance).unit *RefrenceWeapon.Range.Value)
local Collision, CollisionPosition = game.Workspace:FindPartOnRay(Projectile, ProjectileBlacklist, false, true)

--Projectile (beam that comes out of the gun that's messing up)
ProjectileTrail.Size = Vector3.new(0.05, 0.05, ProjectileDistance.Magnitude)
ProjectileTrail.CFrame = CFrame.new(ShootPartPosition, CollisionPosition) * CFrame.new(0, 0, -ProjectileDistance.Magnitude/2)

image
image

My guess is it has something to do with the way you’re handling your CFrame. Try packing your CFrame position math into the first CFrame and remove the multiplication/offset CFrame.

Also, you can do with changing your size math to be a little more reliable…

-- CFrame position

CFrame.new((ShootPartPosition+CollisionPosition)/2, CollisionPosition)

-- size

Vector3.new(0.05, 0.05, (CollisionPosition-ShootPartPosition).Magnitude)

Are you sure that the shoot part is welded or joined to the gun someway?
And yeah, I agree with @BraxbroRoblox , and also try to use the new Raycast since the old one is deprecated.