Bullet Origin offset from where it's actually is

I am having an issue where the raycasted bullet of my gun is coming from where it isn’t instead of where it is.

robloxapp-20250308-2330470.wmv (3.3 MB)

I believe this has something to do with a dynamic body movement script that i have in the playerscript, here’s that and the code where i determine where the bullet is coming from;

Dynamic body script:

My current script:

(this is in serverside script)

local tool = script.Parent

--Raycast
script.Parent.Shoot.OnServerEvent:Connect(function(player, mousePos)
	local bulletReplicator = game.ReplicatedStorage.ReplicateBullets
	local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {player.Character}
	raycastParams.FilterType = Enum.RaycastFilterType.Exclude
	tool.Handle["ShootSound"]:Play() 
	
	local startPos = tool.Handle.Barrel.Position
	local velocity = 1650
	local range = 800
	local direction = CFrame.new(startPos,mousePos)
	direction = direction.LookVector*velocity
	
	local raycastresult = workspace:Raycast(startPos, (mousePos - startPos) * range, raycastParams)
	
	bulletReplicator:FireAllClients(player.Character,startPos,direction,range)
	
	local startTime = tick()
	local lastPos = startPos
	
	local rayFilter = RaycastParams.new()
	rayFilter.FilterDescendantsInstances = {player.Character}
	rayFilter.FilterType = Enum.RaycastFilterType.Exclude

Note: to be more specific, I am also using an animation while the gun is equipped, and the torso of that animation has no keyframes, in the animation editor it looks like the gun is facing the right and somehow shooting the middle, but combined with the camera script it looks like it’s facing straight, or mostly straight.

I believe the issue is at startPos, the tool.Handle.Barrel.Position is actually the middle of the model, you can see how this is bellow.

image

Consider making an attachment inside the barrel and using it’s world position as the startPos instead. The attachment can be positioned anywhere on the barrel, so just put it at the tip.

You can then replace the code with this:

local startPos = tool.Handle.Barrel.Attachment.WorldCFrame.Position

It didn’t work, to be more specific, I am also using an animation while the gun is equipped, and the torso of that animation has no keyframes, in the animation editor it looks like the gun is facing the right and somehow shooting the middle, but combined with the camera script it looks like it’s facing straight, or mostly straight.

The reason why the bullet is offset is because what the server sees as the gun’s barrel is different than what the client does, making it offset. So instead of defining startPos on the server, find that position in whatever client script handles bullet cosmetics.

Speaking of that, you should also consider making the bullet casting client-sided with sanity checks on the server. Otherwise, what players see on their screen isn’t the actual hitboxes, but where they were a moment ago.