I’ve discovered when making my game that destroying a script (via :Destroy()) will cause a lag spike. I can only observe this issue only in Roblox Studio, where a Script in ServerScriptService causes 1 lag spike, whereas a Script in Workspace causes 2 (I assume due to Replication). Its a very annoying bug for testing, You can replicate by making a script in Workspace/ServerScriptService and using the code below:
I’ve previously mentioned this issue in this post: "Write Marshalled" lag spikes in microprofiler - #9 by CrafterRBLXGo but I wasn’t certain of what made it occured as WriteMarshalled could be anything.
This is not only a bug that has happened to me but to many others as well, I’ve asked to my friends to do the same thing and they indeed got a lag spike (so did the people that liked this post), and this seemed to happened a month ago for some like this person: "Write Marshalled" lag spikes - #5 by CyrusDevs
I can totally confirm that destroying a script in a server-only directory lag spikes once, and doing it in a directory that can replicate (like ReplicatedStorage) lag spikes twice.
Also as CyrusDevs said, even if that script is disabled, it still does that lag spike.
A weird thing to note is that it only lag spikes once, that means after that if you try to delete another script, it no longer lag spikes.
This issue started happening recently for me as well, I thought it was an issue with one of my scripts until I narrowed down the problem to one line of code that destroys a part (in workspace) that is parented to a script.
I found the way to make it almost completely unnoticeable is to get one Script and one LocalScript, and put the following code into both:
task.wait()
script:Destroy()
then put the Script anywhere the server can run scripts, and put the LocalScript in StarterPlayerScripts or StarterGui. Both lag spikes will happen immediately before the client loads in and should not happen again if you destroy more scripts in that session.
Using only a Script in workspace will not stop the client lag spike as I believe it’s destroyed before the player(s) join(s), though you could script a system that destroys a script every time a player joins the game
We’ve evaluated your report and tried multiple times to reproduce the issue in both Studio and Client.
We tried both the place file from the original report - RuntimeErrorBug.rblx as well as using testbug.rblx from another developer comment.
Unfortunately, we are unable to see any lag spikes related to instance destruction.
If you have better examples, especially cases where the issue happens repeatedly, that might help, but at this moment we don’t have anything to act on.
Can confirm this is the case for me as well. I was using the script as a throwaway one-time visual effect, and my current solution is to delete all the parts rather than the whole model (script.Parent). This works for me, but is not going to work in everyone’s case. The other repro files cause it for me.
i5-13600KF
RTX 3060 Ti
32GB DDR5 RAM
Windows 11
Beta features:
Assistant Mesh Generation
Avatar Joint Upgrade
Dragger QoL Improvements
Face Capture
Haptic Effects
Import Queue
Improved Constraint Tool
Incremental typechecking and autocompletion in studio
Live Animation Creator
Multilayer Wrap Fix
Next Gen Explorer
Next Gen Studio Preview
Preferred Text Size Setting
Revamped Asset Manager
Script Sync [Early Preview]
Studio solid modeling improvements
UIDragDetectors
Unified Lighting
Video Uploads
task.wait(3)
print("lag spiking now bruh")
script:Destroy()
it’s important to note that it’s probably a good idea that you walk around and have ur camera facing your avatar (like in my demonstration video) System Information: I9-13900K RTX 4090 80GB Ram Win 11
This is happening in Studio’s play test, unsure of a published live game session.
I typically don’t keep stray scripts under random models, but this is very annoying when it happens as deleting the model will cause this; This is not isolated to destroying the script directly. As bad as 153ms frame time upon destruction in my case.