The server is gettting slowly getting lags when shooting projectiles

So the server is getting very laggy slowly overtime when shooting projectiles, like when i press a key, the projectile part is delayed

here’s one of the projectile code. (Serverscriptservice)

game.Lighting.Shoot.OnServerEvent:Connect(function(plr)
local char = plr.Character
				local brick = game.ServerStorage.PistolAmmo:Clone()
				char.Recon.Handle.Shoot:Play()
				brick.Anchored = true
				brick.Parent = workspace
				brick.CFrame = hrp.CFrame
				brick.Position = char["Right Arm"].Position
				brick.CanCollide = false
				brick.Owner.Value = char.Name

				game:GetService("Debris"):AddItem(brick, 8)

				task.spawn(function()
					while wait(0.01) and brick do
						brick.CFrame = brick.CFrame + brick.CFrame.LookVector * 6
					end
				end)
end)

And also some while wait() inside of serverscripts

EDIT:
after putting print() on the projectile
image
the “gogogo” print keeps increasing. How do i fix this?

1 Like

um there’s a lot wrong with your code but thats besides the point;

this problem would probably just be caused by network latency. there’ll always be a delay between the client sending signals and the server receiving them

change the while wait(0.01) and brick do line to

while wait(0.01) and brick and brick.Parent ~= nil do

here’s some other good resources

1 Like

To effectively get rid of this, you can do these solutions:

Handle the projectile entirely on client. Including hitreg:
secure the data on the server though, then fireallclients BUT the plr that sent, to replicate the projectile visual. Don’t do damage on the client either.
The best way to provide a good user-experience, unless you make a custom character (like chickynoid) and history tick system (look at Valve article about lag comp).

If you want server side hitreg, OK. You can do these two options:

  1. When you shoot the projectile, in the onserverevent, instead of having projectile visuals on the server, straight up fireallclients, do visuals there, not hitreg.

  2. Right before you fireserver for the projectile, do the projectile VISUAL on the client that would be firing the remote. Then, on the server event, again, don’t handle visuals. Replicate to all clients but the one who fired the event.

An example to replicate to all clients that aren’t the one that sent the remote:

Remote.OnServerEvent:Connect(function(plr)
    For _, Client in game.Players:GetPlayers() do
         if Client == plr then continue end

         Replicate:FireClient(client)
    end
end)

(May be incorrect, I’m writing this at school)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.