How can I make my gun system not lag?

So I have a blast system for my game, where players fire at each other using spheres. I used body velocity for this system. The problem is, at first, the bullet lags because it is just placed, then it starts moving. This makes the gun harder to use and not enjoyable. Is there any way I can fix this?
My code:

FireBallClone.CFrame = Char.HumanoidRootPart.CFrame
FireBallClone.Anchored = false
FireBallClone.Parent = workspace
FireBallClone:SetNetworkOwner(nil)
local BodyVelocity = Instance.new("BodyVelocity")
BodyVelocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
BodyVelocity.Velocity = Mouse.lookVector * 30
BodyVelocity.Parent = FireBallClone		 
Debris:AddItem(FireBallClone, 10)
1 Like

This isn’t exactly lag, rather something to do with the physics engine. One thing you can do to alleviate some of this is to apply the force before parenting the fireball. I’m clueless beyond this though.

Usually changing the network owner of a part causes it to freeze for a moment. Have you tried setting it to the player or cloning a part that already has the network owner set to nil?

I have these parts in replicated storage, and you can’t set network owners in replicated storage.

What if you stored the bullet inside the gun model and welded it? Then at the start of the script it sets the network owner, and when the gun is fired it clones the bullet and calls breakjoints?

I have scripted a gun in the past but I used cframe instead of body movers, so im not completely sure if this will work.

3 Likes

This is due to sleeping physics. Even if you have Network Ownership, the parts will sleep when Instantiated initially. The only way to resolve this is to have your physics part be created locally. What I would do is have a server-side copy and a client-copy. Use the server for hit validation and client for display.

I managed to fix some of it by putting the body velocity before the parenting, and that seems to help a lot!

I would not recommend to use roblox physics for things that require something like this, it’s better to calculate the projectiles yourself.

You might find this post helpful;

2 Likes