Game is unbearably laggy whilst playtesting

I’m developing a semi-realistic fusion reactor simulation. Recently I’ve been having this strange issue where whenever I playtest the game, it becomes laggy to an entirely unplayable extent. Specifically, the CPU time oscillates up to over 200 milliseconds. I’ve seen it go up to 1000 before.

I have absolutely no idea why this is happening and would really appreciate some help debugging. I’ve used barely any freemodels (only a low poly mesh asset pack) and have double checked all the scripts to ensure there shouldn’t be anything lagging the game that I can’t disable.

If anything, the most performance heavy script is the reactor simulation code that handles simulating the physics for the reactor. It runs calculations at a rate of 3hz, but this is extremely important to the game and cannot be removed.

Here’s a video of how bad the lag is. It seems to get worse for the longer I playtest, it starts out smooth but eventually the CPU time goes to well over 1000 milliseconds.

I’m completely out of ideas and if anyone could help with this it would be so much appreciated. Thanks!

It runs calculations at a rate of 3hz

Since your script is running at 3hz, did you try compressing the script?

I saw your video, and when you first play, it lags, and then after a while, it lags more. For the startup lag, you can use the method “Minification”. It’s commonly used in JavaScript for reducing webpage load times when you enter a website domain. Basically, keep a copy of the script, but make the variables short like 1 char long. You can search it up.

For the lag where it increases the longer you play. It HAS to be something with your code or algorithm. Look for loops that may be recursive. Also look for items that are created but “abandoned” and not garbage collected. Also try reusing created items if possible.

Lastly, if applicable, did you forget to disable cast shadow, can query, and can touch for all the meshes and parts that are for aesthetic purposes only? Make sure you anchor them too.

I will look into minimizing. I didn’t think about that at all. I’m pretty sure it’s an issue with the server though, as the issue only occurs in studio, not when playing the game from Roblox. I’ll try it in a bit and report back.

1 Like

Please don’t forget the rest of my reply.

Lastly, if applicable, did you forget to disable cast shadow, can query, and can touch for all the meshes and parts that are for aesthetic purposes only? Make sure you anchor them too.

And this too, before you minimize:

For the lag where it increases the longer you play. It HAS to be something with your code or algorithm. Look for loops that may be recursive. Also look for items that are created but “abandoned” and not garbage collected. Also try reusing created items if possible.

I turned off CanCollide, CanQuery, and CanTouch on all models for the switches and displays and this helped a ton. It’s now averaging around 80ms, I will try optimizing my scripts and the other things you mentioned as well and see how it helps as well. Thank you so much!

1 Like

These will do nothing to help his situation, this is caused by something extreme, like many unanchored parts or a very intensive script.

Oh, you must be mistaken. I think we literally said the same thing :slight_smile:

Intensive script

Since your script is running at 3hz, did you try compressing the script?

It HAS to be something with your code or algorithm.

Unanchored parts:

Make sure you anchor them too.

Sure! Happy to see things working out! I saw your game, it looks interesting

Compressing or minifying does nothing, code gets compiled on the server. Clients execute compiled versions.

Woah does your game really have that many parts?

If you were using scripts that depended on Spatial Queries you might have just completely killed them, you should double check if everything works alright.

Hi! Maybe you’re right, but I don’t see in the OP that it says things about the client. I know you said that clients execute compiled versions. However, I think the code for the script is executed on the server in the game @universeindex is working on.

How about you provide some help too? I’m just trying to help with all I know. So far in this discussion, I noticed that both of your replies involved correcting me. If you could, please send me a message instead so we can keep this discussion constructive.

Code is also compiled on the server, comments for example do not exist except in the source.

He was just trying to say that Luau uses a ByteCode interpreter unlike JavaScript which directly interprets its own source, and that minifying does not matter here.
There is no one shaming anyone for not knowing a topic, and there should be no shame in that.

Let’s please keep this a friendly thread.

Sorry, I forgot to mark a solution for the thread, but disabling CanTouch and CanCollide (CanQuery is required DragDetectors and ClickDetectors on the switches) really helped a lot. It shocked me how much this decreased CPU time but it definitely worked. Thank you all for the help!

2 Likes

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