Gun FireRate is very laggy

Hola,

So I have gone through a tutorial on how to make guns, due to me having no previous knowledge, and now that I know a bit more, I managed to rummage through and modify it, make it better, my problem now is, the guns are very laggy…

Local: --//By: Mineloxer--//Front end handling, animations, ray casting, hit detectio - Pastebin.com
Server: --//By: Mineloxer--//Handles back end checking + sounds--//Variables\\-- - Pastebin.com

In both I am holding down Left Click.

In Studio:
https://gyazo.com/51b8ece0994117a54b540d686f6940ee

In Game:
https://gyazo.com/1edac3927de5b08760c8cb62047df3c5

6 Likes

You can use the RunService.Heartbeat event.

Instead of the wait(0) on line 156 of the local code you can have

  • Since Wait() usually yields for about 1/30 of a second*

          local DesiredTime = 1/30
          local StartTime = tick()
    
              repeat
       game:GetService("RunService").Heartbeat:Wait()
      until tick() - StartTime > DesiredTime
    

The RunService is more accurate than using wait(number).

3 Likes

Network latency. Create all the effects on the client, not the server (I noticed you are enabling the particles on the server).

1 Like

It’s “laggy” because you’re relying on RemoteFunctions, which yields the LocalScript until that remote returns. If you want a desirable firerate, I would recommend switching to RemoteEvents since that doesn’t yield and does the job.

7 Likes

I’ve created several weapon systems in the past, and I can tell you every single time its the same issue. I used to do the method you are doing, and I highly disregard using this method. Instead of having the server visualize the bullets, make the damage server side, and make everything else client side. What you should do is on the mouse button click, you visualize the bullets right away on the script. It is way faster and there is little to no delay. Right after that, you invoke/fire the server, and the server fires all clients, there will need to be a bulletvisualization script for this, completely separate from the weapon itself. The information the server would fireallclients with is the player who shot the bullet, and all the positioning. The client then makes sure it wont visualize the bullet for the player if the player who shot the bullets name is the same as the LocalPlayer’s name. If it isn’t, then it does the same thing as the weapon does, but uses the positions the server provided. Of course, you most likely will have to have some server sided checks to make sure the gun isn’t being exploited, but most of the gun should be client side, to keep it smooth and in a good working order. Hope I helped!

4 Likes

I created a FireAllClientsWithException method just for this.

network:FireAllClientsWithException(Player ExceptionModel, Variant SendData)

2 Likes

So what should be going on, that paragraph confused me a little?
What I got from that is:

Server :-
Check if tool is activated
Visualise bullet
Fire Clients

Client :-
Tracing
Fire Server

Server :-
Damage player
Take away ammo

The server would not check if the tool is activated, the client would, to make everything more instant. The client would visualize the bullet for the person who is shooting the weapon right away, like on the first shot. And then the server would fireallclients and visualize it for everyone else. Other players can see a partial delay, this is much better than having the client have a delay. If you need an example of how this should look like, I can rescript your script and show you how it should work / look. If you are still confused, let me know. :smiley:

Isn’t that what i’m already doing though just without visualising it on client? I mean, the tracer is playing on client, and that is on client, and also the damage is also a problem as that is lagging also? Can you give a list of what needs to be where, and what fires what.

Thank you, sorry for late response!

I thought about this when making my own weapon system, but wont it look wierd on the other clients side? If the information goes from client to server to client I would guess there would be delay for the other clients.