Gun script, lagging

Heyo,

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 Script:

Server Script:

Any advice on reducing lag would be appreciated!
You can see the lag and see what I mean by trying the guns here:

The more people shooting, the more chaotic the lag.

9 Likes

The game says “The permission levels on this place prevent you from entering.”

3 Likes

oop, fixed

2 Likes

The gun on the local side fires super rapidly resulting in the ammo counter dropping in chunks of numbers.

3 Likes

Do you have any idea of why only one of the guns reloads? (BroomGun)

3 Likes

Could be the other guns are stuck in fire mode and don’t debounce properly.

3 Likes

Wdym?


1 Like

In the loop in the tool.activated function on line 156 in your local script it has a wait(0). That is running the fire() function too fast, it should be at least a 0.01 wait before calling the fire function again.

2 Likes

The difference between wait(0) and wait(.01) is miniscule as wait(0) defaults to wait() which is roughly every 1/30 seconds (or roughly .03), and so calling wait with .01 as an argument is pretty much redundant.

3 Likes

Can you inform us what the configs are set to? In particular what is range and fireRate?

My bad. For some reason I thought wait() just yielded until everything above was done.

Anyway, awhile back I used the same scripts as @aerophobes is using now but I modified them a bit as well (added crosshairs, mobile support, preventing team kills, etc.) and I had a wait(0.01) and my fire rate in the configure folder was set to 300. With these settings I had no noticeable lag. If @aerophobes is interested I can show my scripts.

2 Likes

Sorry for late response:
In both I am holding down Left Click.

In Studio:

In Game:

1 Like

I’ve had this problem too to the point where my Roblox crashed.
Replace your repeat until loop with the RunService.RenderStepped event, like this:

local runService = game:GetService("RunService")
runService.RenderStepped:Connect(function()
    if(mouseDown) then
        -- do the shoot stuff here instead
    end
end)

Hope this helps!

2 Likes