Hello. I am making a press “e” to enter vehicle GUI, and my script is basically looping through all of the vehicles, determining which vehicle is the closest, and then making it so the closest GUI appears, and the rest of the GUIs are invisible. The problem is that if there are alot of vehicles, there is a chance that it could cause alot of lag due to the amount of vehicles that the script needs to loop through in order to determine which is the closest vehicle. Are there any ways to optimize this? One method I thought about is making it so if the loop is taking more than a certain amount of milliseconds to loop through the table of GUIs, we can pause the loop for a couple of frames, and then resume it. I’m just wondering if there is a better way to optimize this.
2 Likes
if you are using :GetChildren() or not caching the vehicles, I’d suggest referencing the vehicles in a cache and listening to ChildAdded/ChildRemoving signals to update the cache
you could tie the update to a framerule/set interval so that the gui checks only update every x seconds
seems unnecessary, all you’re really doing is a magnitude check & comparison. if the update loop takes a long time, then there’s something else causing lag and not an update loop
Ok, that’s what I’m going to do.
Ok.
I haven’t finished scripting it yet, but I was afraid that this might be an issue that I would have to worry about, and so I was thinking of ways I could fix it. But I have no idea if this is even something that I should worry about.