I am trying to make a game with a very large amount of parts, so it is laggy.
I want to decrease lag by parenting parts not in the viewport to nil, however then I don’t know how to track when they are back in the viewport to parent them to workspace.
As far as I know (someone correct me if I’m wrong) this will just make the game even laggier. The best way to make sure that the client has the least laggy experience is to enable streaming (click workspace, tick streaming enabled in properties). This will only load in the parts visible to the player. Youll have to watch this though, as it will break some scripts, especially if you use waitforchild() in a client script. You can also make sure as many parts as possible are anchored, so they arent treated as physics parts. Also, if you go to view–> script performance, youll be able to make sure that scripts arent causing the lag
Having a streaming system based on the viewport itself isn’t really a sound idea, as fast camera movements will result in a delay; i.e. if you do something like turn around you’ll have to see the things behind you now load. Not ideal. I’ll go through the process of a simple streaming system where you can control the parts you want based on their distance from the character
Separate your map into “chunks”. I’ll use a cute little grid so you can follow along since this might be confusing…
That’s your map, the player in the middle, the red outline represents the chunks you want the player to see.
Initially, your entire map should be in a place that both the server and client can access, probably Replicated Storage as this seems to be the only viable option outside of workspace. Now you’ll want to track which box the player is in every so often. The box won’t change often, but it will sometimes:
You’ll send a message to the client to remove the red boxes as the player will be too far from them, and tell it to add in the green boxes since the player will be approaching them. Just send the names of the chunks, you can either index them by name in workspace to move them into replicated storage or in replicated storage to add them in.
How you go about deciding which boxes to add or delete shouldn’t be too hard.
Yes I know streaming enabled exists but I figured this’d be better for your purposes as you’ll be able to choose which parts you want to have being streamed into and out of the workspace, and you’ll seldom encounter troubles with your scripts as if a script really needs to know what a part is doing regardless of its position, you just leave it out of the chunk system.
I suggest measuring out 1024x1024 chunks, just get a rough idea of big squares that you can separate your map into. You don’t have to split up the actual landmass as that’s just one big thing, but group large buildings, any meshes, etc. into those squares.