So I’ve been doing some reading on garbage collection, and destroy()
I’m trying to remove some scripts from my game, to reduce some background lag.
The issue is that I have several scripts running in the main menu (for example) that still run after the main menu gui is destroyed.
I have a renderstepped connection that disconnects when the script is destroyed; Because thats intended behaviour.
However, the script still shows in the script performance tab, with an activity of 40%
How can I just delete the script? or if not delete it from memory, disable it when the script is destroyed? is there an event that I can fire when a script is destroyed?
AND: I’m deleting the whole gui; does this mean that the gui is sent to nil? or does it get broken down piece by piece; I’m asking because the script is nested in the gui, and i’ve tried to check after the script was destroyed, if its parent is equal to nil so i can disable it.
or should I just disabled the script before the gui is destroyed?
I want to make sure I understand this:
If I declare a script that uses many local variables, and destroy it, do those references become nil, or do I need to nil those references at the bottom of my script / when the script is destroyed?
Lets say I have a script with 15 remote connections (Client side) and I delete the script. Will those references stay?
I have a local script in the character, that is removed with the player when loadCharacter is called. If I reset the character 4 times, would there be 15 active instances of those remotes, and 45 others that are just memory hogs?
From what I understand, Calling destroy on a script Disconnects all connections in that script, but simply parenting the script to nil, would allow the connections to continue.
The second the script finishes executing the variables lose the reference, you wouldn’t even need to destroy the script. Variables referenced in functions that connections call would still he alive however and you must disconnect the connections.
If you destroy it, expected behavior is the connections are disconnected.
Not sure what you mean here. I don’t believe LoadCharacter calls Destroy on the previous character.
How do I remove the script reference from the script performance list? The script is only created once, and destroyed too. However, the script is still visible in the script performance list.
After doing some reading, I’ve found this in the api:
I don’t like to assume, cause you know what they say Does ‘removing’ in this context mean ‘Destroying’?
For testing reasons, I created a local script with a runservice.renderstepped Connection.
After the connection was addressed, I placed a script.Parent = nil
To my surprise, the script didn’t continue its execution.
game:GetService("RunService").RenderStepped:Connect(function()
print ("This is running")
end)
wait(2);
script.Parent = nil;
After doing some more thinking, I came to the Destroy API one more time, with a greater scope of whats happening.
what if the script is destroyed by another script?
Additional Info 1-------------------------------------------------------
So I did some digging around, and I came across this bit of code that should apparently check if the script was destroyed; but it doesn’t print.
script.Parent.AncestryChanged:connect(function()
if not script.Parent:IsDescendantOf(game) then
-- item was removed
print ("Destroyed main menu")
end
end)
Okay, so I randomly came across collectgarbage() in the script prediction dropdown.
But I can’t find anything in the api, and I get an error when calling it because I don’t know how to construct the argument(s) required to make it work.
collectgarbage(); [Workspace.Paradigm_Dinosaurs.ClientGameManager:188: bad argument #1 to 'collectgarbage' (invalid option 'collect')
collectgargbage in vanilla Lua lets you explicitly invoke the garbage collector. You can’t do that in Roblox, the only reason that collectgarbage still exists is so that you can call collectgarbage('count') to get the current Lua memory usage of the game.
which I was about to say, that I can get count to work, but thats it so far lol.
JW, Why cant I collect garbage myself?
How often does roblox collect garbage?
The reason I want to collect garbage myself is because when the player restarts a stage, instead of figuring out a way to reset every script that is updated, I just destroy the whole folder; and recreate it. I noticed when I do that the scripts do not leave the script performance tab.
Also, when a new stage is loaded, those scripts are no longer needed; unless they decide to go back and play that level again later. but those scripts shouldn’t be in memory anymore.
Because people are much more likely to make things worse by trying to fiddle with it than actually improve things. There’s just not enough information available on the Lua side to make better garbage collection decisions that are already being made automatically.
collectgarbage doesn’t change the garbage collection behavior at all, only the performance, so it wouldn’t be helpful to you anyways. This sounds like a behavior problem, and one which has nothing to do with garbage collection at all, since garbage collection is happening all the time at a reasonably high frequency, if it were a garbage collection artifact it would be hard to even catch it in the statistics panel because it would be so transient.
I don’t know the behaviors for deleted scripts off the top of my head, I always write everything as a single client side and single server side script using a bunch of modules to avoid exactly these kind of issues.
The thing is that I have many objects; in, so far, a few stages. Each object has its own properties and running them all in a singular script became very messy. Its much more elegant, and convenient in the method I’ve set up; Besides this unknown behaviour. in theory, it makes great sense.
I just need to know why the script performance holds the old scripts. And their activity and rate are still running all the while too; Thats a game breaker right there
So after some more testing, I’ve been trying to do this:
local currentP = script.Parent;
while script.Parent == currentP do
print (script.Parent.Name)
wait();
end
print ("Gems exit correctl;y")
but I can’t get it to print when the parent has changed;
I am attempting to make the parent nil and then destroy them afterwards.
Can anyone tell me what the heck is the solution? X(
So after much analysis I’ve concluded that the scripts activities are at a standstill when they are destroyed; When I load the next stage the next set of scripts are loaded in.
When I go back to stage 1, the scripts that were destroyed begin running again, and stage 2 scripts are at a standstill. And so on.