How to Squash "ghost" Bugs

We’ve all been there, hours wasted, trying to figure out an obscure bug that doesn’t print errors, coming from a script that looks fine and dandy. While these enigmas may seem scary and even unsolvable at times, here are some tips and tricks I use to conquer these extremely annoying code-roadblocks easily and efficiently.


After a few years of scripting experience, and much time trying to help people in #scripting-support, I have encountered many instances of this gosh-forsaken boogeyman. Nevertheless, I’m human too and this is my first tutorial so I may make a few mistakes, please just bear with me.

So, what are these “ghost” bugs I keep mentioning?

  • I describe ghost bugs as bugs that don’t error in the output, or are caused by some strange behavior of Roblox Lua.

  • Bugs whose output make so sense

  • Other very obscure bugs that don’t happen in studio

  • These often take hours to solve and are usually super frustrating because of their obscurity.


Before we Begin

Although this only applies to about 2 people, if you’re running into issues with your scripts, make sure you have the output open to help find bugs. In addition to this always look up your bugs because I can guarantee you that 90% of the time the answer is already on the internet somewhere, you just gotta search for the right things.

Also,

I know this tutorial has probably been done before, but… I cannot find any guide on this topic at the time of writing.


Now for regular bug solving tips that print outputs, read below

Regular Bugs and Extra Tips
  • Look up the error code.
    This may be obvious, but many errors can be solved by simply looking up the error codes on the internet

  • Extra Advice
    If you’re having trouble figuring out how to do something, or it’s not working correctly, take a quick break and then try thinking about it again. Sometimes I’ve tried to do something in a super advanced way, then finding out later it could of been done more optimized and less time consuming.

  • For Beginners Using Remote Events
    I was probably the only one who made this mistake but I thought i’d include this anyways. Keep in mind the first parameter in “remoteEvent.OnServerEvent” is always the player object of the player who fired the event.


Alright, and now for the main attraction… drumroll please

Finding “ghost” bugs effectively


  • Use print functions to make sure the code is actually running
    Many times the cause of these ghost bugs can be solved by checking if the code is actually running. Many times you may forget to run a function or the method to start it is not firing. (An example of this is MarketplaceService.ProcessReciept which can only be run by one script).

  • Check your conditional statements for mistakes.
    Sometimes if you’re using :FindFirstChild() or if statements you can make spelling mistakes in strings or variables.
    What are Conditional Statements? Conditional statements are “if this then” statements.

  • Proofread your code
    I can’t tell you how many times I’ve spent hours trying to figure out a bug, and it ends up being something like forgetting to connect a function, or setting a value, or using the wrong name. Proofread your code.

  • Make sure you’re using the right functions
    Over and over I have seen users in the scripting-support section ask things that could be solved with a simple google search or making sure that you’re using non-deprecated functions.

  • Check Replication Issues
    The server and the client are not the same, destroying something on the client won’t destroy it on the server in filtering-enabled scripting. Always make sure this isn’t an issue.

  • Check variable spelling
    If you have a variable that was not a global local (using local outside of a function) in multiple functions, it may become a global variable.

  • Check for looping multiple times
    This has happened twice to me and the first time it happened I had no idea why. (more info here). You must make sure you’re debounces are working and not getting bypassed by low-interval calls. I usually fix that by disconnecting the function temporarily but moral of the story is you gotta make sure you’re not having multiple of the same loop running simultaneously if you don’t wan’t it to.

  • Check if another script of roblox function is causing an issue
    Things like streaming enabled can break localscripts or cause some weird bugs if not scripted correctly. On the other hand, scripts in the game can manipulate the workspace is weird ways. Basically, just make sure you still have everything the same when trying to edit it.

Extra Advice (from me)

Controversial Advise (check comments for the ones that are proved wrong or fixed)
  • note: this is controversial and comes from my experience and opinion, keep this in mind.

  • Avoid “while wait() do”
    I don’t know why this happens, but every time I and even many people in #scripting-support use “while wait() do”, the loop ends up not running at all. In addition to the fact that you really should avoid wait() if possible, you may want to use a good old “while true do” or a runservice event.

  • " function_connection:Disconnect() " only works half the time
    In my experience, :Disconnect() only works about 30-50% of the time by itself. When combined with “connection = nil”, it works fine. (I even had connect = nil by itself and it worked fine).


  • If all else Fails, Rework shall Prevail.
    Now rewriting code is a very scary prospect for most, as people don’t wanna spend time doing the same thing again. Yet, on the other hand, sometimes rewriting code is faster than actually trying to figure out what is causing the bug.

Thanks for reading this little tutorial, I am always looking to help others with their problems and questions so if you have any questions, suggestions, tutorials or open source modules, tell me about them here.

To help me better the quality of tutorials such as these, please rate this tutorial out of 10. Also feel free to leave a like on this post.

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

0 voters

30 Likes

Thanks, I’ll be sure to bookmark this for when I come upon these bugs.

2 Likes

RbxScriptConnection:Disconnect() always works 100% of the time unless you’ve encountered what is possibly a critical Roblox bug or are using a custom object. The connection should always be broken 100% of the time.

But if you have a connection stored to a variable, it will need to be set to nil afterwards to stop the connection object itself from being kept in memory, as Disconnect does not remove the object itself – but again that only matters if you have it stored in a variable (and this also requires that the variable has a reason not to be garbage collected). If you’re just making a connection on its own then it doesn’t matter.

e.g.

-- doesn't matter because not storing to a variable
workspace.Part.Touched:Connect(function() end):Disconnect() 

-- doesn't matter because variable gets GCed when script is done
local connection = workspace.Part.Touched:Connect(function() end)
connection:Disconnect()

-- DOES matter because connection2 gets used somewhere so won't be GCed when script finishes
local connection2 = workspace.Part.Touched:Connect(function() end)
game.Players.PlayerAdded:Connect(function()
   doSomethingWith(connection2)
end)
connection2:Disconnect()
connection2 = nil 
5 Likes

Someone should make a plugin that inserts print statements in between the lines you want it to, I think this will help a lot with ghost bugs.

Nice job. This is especially helpful for beginner scripters who are unsure of how to proceed.

2 Likes

Edit: More tips and tricks added! Feel free to give feedback on them if you would like, it would be much appreciated and helps me improve.

I really enjoyed reading this, and learned some useful tips too. Amazing post!

1 Like