How to make a game lag less

What do you want to achieve? So i made a gun system that appears to experience a lag spike every time someone gets killed or shoots alot. How can i “optimise” the scripts or have a way at reducing lag and making it smoother and faster?

What is the issue? Lag spikes and drop in performance when using guns.

What solutions have you tried so far? I have tried adding waits so it has a cooldown to make an attempt at reducing lag. I have Streaming Enabled turned on but im not sure if that changes anything.

EIDT: I’ve looked at the Performance stats in game and the game is using about 956 MB of memory, The ping is quite low at a base of 50. Does anyone know how i can reduce the memory used?

21 Likes

You can’t just expect others to solve some issue, without providing anything to analyze, which in this case is the code.

5 Likes

So your implying i send you my entire gun system? I cant send snippets bc i don’t know what parts are causing the lag in the first place

4 Likes

tbh is pretty impossible for a “big” game to use less than 700MB of memory independent of what you try, even when you make a game without anything it will use allot already over the “flag” that roblox has set which is 200MB

7 Likes

But my game is quite empty with only a few parts in workspace and a gun system so i dont know how its using over 900mb already.

3 Likes

then i would recommend to use coroutine.wrap, task.spawn or spawn(pretty old)

3 Likes

If you feel uncomfortable sharing it, you don’t need to. But additional info needs to be provided, such as when the lag occurs, whether it happens in general or when you do a specific thing like reloading. Other causes can be poor or unoptimized code.

3 Likes

but if you don’t give more informations about your script we can’t really help you

4 Likes

I have stated that the lag occurs when using the gun in general or when u get a kill. I believe its because the code is not optimised as i have never learned how to optimise my code before. If you have any advice / help for me on how to optimise my code, could you let me know?

3 Likes

How would i use these? Ive never used them before.

3 Likes

If I don’t even know how the code look like, what advice can I give :sob: ?

3 Likes

What piece of the code do you need?

3 Likes

You could maybe give general info for optimising any scripts such as how to normally optimise scripts.

3 Likes

Well it would be better if you could pinpoint the code which is causing the lag by commenting certain parts of your code and seeing if the lag goes down, and if a culprit has been found, just post the code here

2 Likes

The gun is ran by 1 local script and 1 server script using FastCast in ServerStorage. My question is how by commenting does it make lag go down? I feel as though that would not fix anything.

1 Like

Situation update:

I replaced all the Wait()s with “task.wait()” and deleted a dummy that lags when u kill it from the game and ive brought down the memory a bit and its freed up like 100+ MB
image

I have noticed that some people use a semi colon after a line for example

end;

-- or even,

end);

i was wondering if that means anything and if it affects the script in anyway?

You may want to check microprofiler for the reason of the huge latency between frames, it can help you identify whether it may be a physics issue ,or identify the group of scripts causing the issue.

3 Likes

Doesn’t seem like much code to cause performance issues.

While, ofc task.wait() is superior to wait(), I don’t think it will cause such a noticeable performance drop.

That’s optional in luau, and it has no impact on performance.

1 Like

It might be the fast cast module the gun script relies on to work.

FastCast is a well tested module, so I don’t think the issue comes from there. You could open up micro profiler as such: