Hello, everyone. Thanks for coming here!
To be clear im making a script that uses Intstance:GetDescendants() in RunService.RenderStepped event, and it has been very laggy, freezing the screen, or even crashing the studio.
Summary:
What do you want to achieve? Interact with instances using Instance:GetDescendants() without any lag while running it in RunService.RenderStepped event.
What is the issue? When the script starts the RenderStep event, when trying to get descendants from instances is very laggy,
What solutions have you tried so far? Replacing the getdescendants() with getchildren() and looping through the instances, haven’t found any topics related to this issues on dev forum.
Also i have a video footage, (in my case) the event is fired when a tool is equipped, and the rest you can see…
I will be really glad if someone got a solution for this, i will appreicate any comments, and any suggestions / ideas !
Have you (tried to) use the MicroProfiler to confirm this? Also, I’d like to see the code. (Also this should probably be moved to #help-and-feedback:scripting-support)
Out of curiosity, why are you using GetDescendants every RenderStep? It would be more efficient to use it once and cache it as a variable. You could then use DescendantAdded/Removed events to keep the list updated.
No, haven’t tried using micro profiler, ill look into it, let me move this to the topic you mentioned as i’m kinda new to forum and i am not sure where do i post it.
yes but this is not really efficient in my script, lets say it stores the cframe of all the parts from an object into an aray, and it has to be updated every frame.
Well, im not sure how will i be able to use the micro priofiler though because studio freezes for about 1 minute each time i test the game and equip a tool…
Sure, but its really complicated and its a whole system so let me simply clarify what it does:
imagine you have an array of every single descendant that is in workspace, and then you call renderstepped event and loop through that array, lets say to change the color of the instance. and due to a massive instances count, it bugs the whole studio, freezing the screen.
(sorry i really cant show the renderstep function as its not gonna be really possible to understand what its used for, hope u can understand a bit from what i said)
Yea that’s probably why it’s so laggy. I think it would be common sense to NOT loop through every descendant of workspace between frames. I could suggest using a (temporary) timer for the render stepped, skipping each step until the timer hits 0, and then resetting the timer.
Why would you even need every descendant of workspace? The least you could do is take advantage of branch prediction (probably) and skip any instances that aren’t BaseParts.
Lets say i use the script to record the object movements every frame, so then you can like travel in time and reposition the object to their original location that was registered on that specific frame, making a kind of, replay sequence or a recorded video???
Ah, I see. I get what you mean. This can easily be optimised though, but I understand not everyone has excellent problem solving skills.
Similarly to what sleitnick said, you can start by taking the positions of objects in at the very first frame, and then, in each step, check if that object has a different position. If it does, run whatever code, and if it doesn’t, continue through the list.
Additionally, it would be much better to use Heartbeat, because that’s the rate at which physics runs (usually when objects move), and it would be bad to run unnecessary code every frame.
Also, I’d add a blacklist for the player character and it’s descendants, since those can be manually rewinded. If anything, I’d actually just whitelist specific objects that you want to rewind (to exclude structural parts such as the baseplate). The key with this system is to avoid checking as many parts as possible.