How do I prevent lag on a cloned object (ex: bullet casing ejecting out of a gun when shooting)?

I made a gun with ejecting bullets, but when I shoot it, notice how the bullet freezes when it gets cloned into the workspace for a second, then it acts normal again. I wanna get rid of that lag freeze at the beginning, how do I do this? The main problem is because of lag, and I don’t know how to get rid of it.

EXAMPLE: https://gyazo.com/208caa66844229e08be76273805f5e34

3 Likes

If you’re okay with only the shooter seeing the shells, one thing you could do is just clone it via a local script (or fire a remote event to the client if shooting is handled on the server.)

what you can always do(and that fairly reliable) is a client-server-client configuration, where the server has an invisible projectile, and the clients have the visible part of it
i have it in my game and so far i really havent run into any issues(aside from the occasional miss since im using roblox’ physics)

If no solutions are available for the shells to be lag-free in the server, then yes, I will use local instead.

If the projectile is invisible, and the clients have the visible side of it, does that mean the server has the invisible side of it?

Do it all client side. The hit and damage calculations are done server side. So basically you do all your eye candy and play your game by your self until you ask the server to hit someone please. It then does the shot and then if it hits it tells you you hit. And yay your computer is allowed to show you that you hit.

You could use two methods.

One you could transfer network ownership of the shell to the player initially shooting the weapon, a thread to that can be found here.

Another method you could do is possibly have a module, that replicates the shell shooting out of the weapon, and fire that across all clients within a radius. The reason I say this because if I am not mistaken Roblox servers handles the majority of its physics, the more unanchored parts the server has to calculate the laggier the server, so what you want to do is distribute that effort across the clients so that their computer calculates the physics instead of the server. You’d have to have some experience in modular scripting but it could be possible to fire all clients every time the event that handles the projectile is fired.

Link for the second method

3 Likes

to the two that previously answered before me: never trust the client, its the worst mistake you can do

Could you show us the code of your gun? So we can help you a bit.

The OP only need shells with the shell casings that eject out of the gun, not actual bullets. Unless OP plans to also use this message for actual bullets, then it should be fine to make some things client sided.

Try using CollectionService then when the Tag “ShellCasing” is added to an object handle that on the client and make a clone on the client it will end up looking more smooth on the client but it is not what the server sees.

Sure. It’s a really simple piece of code:

local DebrisService = game:GetService('Debris')
local shell2 = game.ReplicatedStorage.bullet
local shell = shell2:Clone()
shell.CFrame = script.Parent.Bullet.CFrame * CFrame.fromEulerAnglesXYZ(1.5,0,0)
shell.Parent = game.Workspace
shell.CFrame = script.Parent.Bullet.CFrame
shell.Anchored = false
shell.Name = "Shell"
shell.Velocity = script.Parent.Bullet.CFrame.lookVector * 35 + Vector3.new(math.random(-10,10),20,math.random(-10,20))
shell.RotVelocity = Vector3.new(0,100,0)
DebrisService:AddItem(shell, 5)

I put it on the client side for now, because I’m still trying to solve this lag issue.

You might try use RenderStepped/Heartbeat/Stepped?

shell.Parent should be at the end, because u unanchor after. that small amount of time might be causing it