#Lag spikes with Internal ScriptContext
1 messages · Page 1 of 1 (latest)
theres a script that probably causes that
inside of a script, what can cause that number to go up?
I dont understand what internal script context even is
screenshot where
i agree with ve
what am i screenshotting????
Theres no error code, and i dont know where in the code this is being caused
I dont know what internal scriptcontext is
and its spiking in the dev console
screenshot that
okay 1 sec
lemme play so i can get it to spike
rn its kind of normal ish
its only doing 1000 spikes
but when i play the game through roblox and get past wave 10 it starts to do 5000 spikes, lag and reset
memory usage?
i lost so it reset, im moving into the game through roblox
i think memory was above 2500
let me get to wave 10 when it starts spiking, then ill send across all info thats spiking
i meant is this your memory usage, looks like it
yes
its under the first tab
which is the memory tab
this section
i take it the 1500 is this internal scriptcontext?
i think so yes
rn on wave 5
1800 spike
goes back down to 80
back up to 2000
it looks like garbage collection cycles, you have a memory leak until roblox streams out some things
due to low memory situation
how does this fix??
it was happening to my playtesters too
rn its peaking at 4000
ya thats what memory leak looks like
its hella laggy
you probably don't dispose of some instances until low memory situation causes roblox to clear them out
what instances is that, i have projectiles, but i destroy them after they are used
is there a way to test for things that arnt being disposed?
any pointers on things to look for?
any strong reference to an instance will keep it in memory
Is it just instances that dont get destroyed or what causes memory leaks?
destroyed instances stay in memory if there is a strong reference to it
what is a strong reference?
any reference that is not a weak reference
how do i know which is weak or strong, and what is the difference?
a reference is strong when it isnt weak
can i have an example of a strong reference compared to a weak reference
local x; x=instance.new()
is that a strong reference?
references are strong by default
how do you make a reference weak?
metatable
okay understanding
so lets say im storing something in memory, if i destroy it, its still in memory, so how do i remove it?
so even after destroying it i have to set it to nil?
most of the time yes
so hold on
other than towerSpawn, would i need to set the other to nil?
and or, is it good practice to always set it to nil anyway, even if its not needed
depends what it is and where
okay, so im not good at finding memory leaks as iv just been introduced to in roblox, BUT
lots of different things can cause it
common one is not clearing tables of bad data
nail on the head for what im about to send
local blacklist = {}
idk what that is
if you never remove things from the blacklist then yes this is a huge issue major memory leak
it's not even the parts are being destroyed, it's that you are inserting duplicates and repeated copies of everything, every frame
RenderStepped:Connect( table.move(blacklist,1,#blacklist,#blacklist,blacklist:clone()) )
okay, so, for my game i need this to run everyframe and to add to it as when you are placing towers, it adds to blacklist based on what your hovering over when you are spawning a tower
basically you're inserting a full copy of whatever is in the blacklist into the blacklist again every frame
meaning the size of that thing go crazy
and you never clear it
thats probably why it scales more at later waves
you probably dont need to run this every frame
as there are more towers to add and not remove
is there a better way around it, the table.inser for towerSpawn blacklists the tower itself to prevent it from flying at the camera
quickest fix i can see is just reset the table at the start of each frame to rebuild it, but tbh this pattern is pretty bad, but eh you're learning and this is how you learn; you try something, it fails, you learn why, and do better the next time.
the selectedTower one removes anything that will interfere
other options are something like checking if something is already in the table before adding it
which is why i ask so many questions, sorry if its daunting
listening for instance destroy to remove it from the table
etc
or use weaktable
etc etc
the only daunting thing here is the time i would need to spend to tutor you and there's plenty of other people here who can do that for you 👍 imo you should be able to learn what you need by just searching weaktables at this point, but 🤷 you can do whatever you want
i didnt even know that weaktables is a thing
im probably not going to get tutoring, but just incase i do get to a point where i really cant do anything else, where would i find them?
ya most people don't understand what they are until they need it
you don't need a weaktable here
because you're inserting duplicates. weaktables is for a different problem; if you had dead instances in a table but kept the reference alive
i.e, tbl[any] = instance and you just left it there without removing the entry
or tbl[instance] will keep it alive too.
but again... that's not the issue you're having
im assuming this had something to do with a solution, are you able to explain this code
iv never used table.move before
nor used it on a connect
for _,v in table.clone(blacklist) do table.insert(blacklist,v) end
also, not to glaze, but you have been a massive help every time you have came into my threads, even if its annoying that sometimes you leave me at a point that forces me to resolve the situation myself without further help, i can tell it has benefitted me
im guessing the point is to clone the table and then remove it after iv placed the tower?
wat no i was just describing what your code is doing
you're adding a copy of the entire blacklist table onto the end of the blacklist table, so things like players are in there 1000 times for no reason
after enough time this will eat 100% of memory and if it weren't for failsafes, this would crash roblox and/or the user's device.
this makes sense, im going to try and move these out of renderstepped
but if there are any i cant move out of renderstepped, how would i go around clearing the table
like if all you did was add blacklist={} to the top of renderstepped before populating it again you wouldn't have a memory leak
because the contents of the table from the previous frame(s) get cleared
table.clear() is another function for this
same same
i know that, but i removed from there yesterday so i can use the blacklist in other parts of the script
well ya i can't do everything for you. and if i hold your hand the entire way, where are you going to have your own small victories? robbing you of that experience is the most unhelpful thing someone can do; at worst, outright giving you working code and you learn nothing, yet there are those who genuinely believe making you entirely dependent on them to complete scripting tasks is what 'help' is.
so i'm glad you have it in you to solve your own problems, that's scripting 👍
thanks bro, but i do still want to ask questions as i solve this, because the main thing im struggling with now is that im trying to figure out how to make the blacklist work with your idea
if i move the blacklist creation back into the renderstepped, then how would i use it across the rest of my script
hahahaha yeah now you learn why the pattern you were using never worked in the first place
it do be like that
and because you relied on this pattern everywhere, now you have to refactor
or do some cheap trick just to make it work
realistically, does the table.insert for blacklist NEED to be in renderstepped, surely theres a way to do it once, or just do 2 seperate blacklists
this is known as technical debt
but how do you join tables to make 2 seperate ones
where you need to go back and fix bad decisions you made much earlier in development

the longer your code gets, the more debt you have, and it is a skill to deal with it well. the only way to do that is by making exactly these mistakes, over and over again.
sometimes you can get a hint
gotta push through
sometimes someone might be like "oh do it like this, it's better this way, trust me bro" and you either go along with it and it just works out and you never learn why, or you do your own thing, and then you become the person telling other what specific way to do it, because you know why it should be done one way and not the other.
it's a, you actually understand why, instead of 'some guy told me to do it this way so i just kept doing it and it kept working idk why it works'
im sure you've seen a lot of those
this is the reason i avoid youtube at all costs, and also the reason i originally gave up on coding a year ago
originally i was streaming my coding, and it was fun, i reached a new peak viewers of 30 and was getting a consistent 20
but the problem was, EVERYONE was always "This isnt the most optimal way"
and the chat was always arguing about the ACTUAL way
and it was so so so overstimulating to deal with
i much prefer learning my own way
create my own method
my own optimum method
collaboration has less synergy then indie development
ya this is premature optimization
it's easier to argue microoptimizations than actually build anything 
i can tell lmao
or just to shut your brain off and simply be told how to do everything
there's a lot of those people around
holy they are annoying, i used to have a friend that was like that
literally wouldnt do anything until they were TOLD to
like a dog
yes, that
'you could do x or you could do y'
'yeah but which one is better and which one should I do?'
😮💨
i think asking which one is better is sometimes okay, but asking which one to do is choice
and half the time, the best decision often changes as needs change, or you end up needing a part mix of both, different situations call for different decisions
or on occasion there's no difference other than preference in how you work with it in other places later down the line... but those people usually don't get that far down the line for it to ever make a difference, so they wouldn't know
anyway, memory leaks like this aren't too common, normally it's keeping dead instances in a table like a table of players and you don't remove the player who left
but now you know to keep an eye out for both, and to not infinitely fill a table without some limits on it
you require to make your own decisions to progress in scripting
yes, i know this now 😭
im gonna keep this thread open incase i rerun into another issue whilst fixing this.
But thanks for all the help and the chat too, was refreshing