Client sided bullet problems

So I’ve been working on a gun system, except theres one big problem. The bullets are way off whenever you shoot while moving. I’ve had this problem in the past with an old gun system I made, and I easily fixed it by just making the bullets appear on the client only. I tried that again this time and it made no difference. Here is my code:

Server:

function shoot(gun, pos)
	if not gun:FindFirstChild("SpecialShoot") then
		local origin = gun.Barrel.Tip.WorldPosition
		local direction = (pos.p - origin).Unit * gun.GunConfig.Range.Value
		local result = workspace:Raycast(origin, direction)
		game.ReplicatedStorage.Remotes.BulletReplicate:FireAllClients(origin, direction)
		gun.BodyAttach.Shoot:Play()
	else
		require(gun.SpecialShoot).Fire(pos)
	end
end

game.ReplicatedStorage.Remotes.GunEvent.OnServerEvent:Connect(function(plr, gun, event, pos)
	if event == "Shoot" then
		shoot(gun, pos)
	end
end)

Client bullet script:

function createBullet(origin, direction)
	local midpoint = origin + direction / 2
	local b = Instance.new("Part", workspace)

	b.Anchored = true
	b.CanCollide = false
	b.Material = Enum.Material.Neon
	b.BrickColor = BrickColor.new("New Yeller")

	b.CFrame = CFrame.new(midpoint, origin)
	b.Size = Vector3.new(.1, .1, direction.magnitude)

	game:GetService("Debris"):AddItem(b, 0.1)
end

game.ReplicatedStorage.Remotes.BulletReplicate.OnClientEvent:Connect(createBullet)

This is what it looks like:

Does anyone know why this is happening or how to fix it?

What is happening is you are sending positional information to be replicated to each client. By the time the client receives this data, the player has already moved, making it appear as though the bullet starts from beside the gun rather than the gun itself.

Try setting the origin to the actual part that you use to determine position. This will make it so that it appears to be coming from the end of the gun regardless of the delay between the remote event and a player moving.

1 Like

I see now. Thanks! There’s still a tiny bit of lag on the bullet, but at least its not as bad as it was before.