How to understand your script bugs

hello scripters, in this tutorial im going to teach you how to understand your game bugs.

i’m gonna use this as an example

(i’m not promoting hate towards this person, it’s just an example.)

if you are too lazy to open the thread, here it is.

step 1: check using print()

the OP of the thread i mentioned did a good job for putting prints after every statement. the purpose is to check what is causing the issue, in this case it is “Touch event not firing”.

step 2: check where did the print end

from the code, we can see there’s 3 print calls: “1”, “2” and “three”

  • print("1") doesn’t have anything can could potentially block its way to print “1” to the output except the .Touched event, which is the one that executes the function attached to it everytime something touches the part. so logically if it doesn’t print, or in other words there’s no “1” in the output, it means that the basepart named tiospike (which is the part that the OP wanted to use Touched on) didn’t sense any touch on it, there’s multiple things that can cause this. for an example there’s another part above the part, that made you touch another part; or maybe there’s a part with a similiar appearance so you touched the wrong part?
  • print("2") has a statement above THAT COULD POTENTIALLY cause your script to not print “2”. the statement above is touchedonce == false, so if it prints “1” but doesn’t print “2”, it means that touchedonce is NOT false. it could either be true, or perhaps something else than a boolean like "ilikesussyballs".
  • print("three") also has a statement above, and it is not hit.Parent:FindFirstChild("Humanoid"). if it fires “1” and stops at “2”, then it means the criteria wasn’t fulfilled. lets see, the OP didn’t mention any errors. so hit.Parent does exists as an Instance, but it has a :FindFirstChild("humanoid"). The OP used :FindFirstChild to find an instance named “humanoid”, not “Humanoid”, but ANY instance named “humanoid”, it doesn’t matter if the ClassName is not “Humanoid”, any Instance like a Part, a Wedge, an MeshPart class will pass only if they have the name “humanoid”. I’m very sure you know the structure of a R6 and R15 rig in roblox right? if you do, you may already spotted the issue here. it’s the :FindFirstChild("humanoid"), because the default rig structure has the humanoid named “Humanoid” with a capital “H”. not humanoid with all lower case.

step 3: fix the code

after doing checks with print(), we spotted the issue and shall fix the code immediately before you forgot

local touchedonce = false
tiospike.SmallSmike.Base.Touched:Connect(hit)
  print("1")
  if (touchedonce == false) then
    print("2")
    if (not hit.Parent:FindFirstChild("Humanoid")) then
      print("three")
      spike1.Color = hit.Color
      spike1.Material = hit.Material
    end
  end
end)

or a better solution using :FindFirstChildOfClass("Humanoid") to find an instance with a classname of “Humanoid”. (WARNING: this function only searches the instance with the EXACT classname and NOT the instance’s name given.)

local touchedonce = false
tiospike.SmallSmike.Base.Touched:Connect(hit)
  print("1")
  if (touchedonce == false) then
    print("2")
    if (not hit.Parent:FindFirstChildOfClass("Humanoid")) then
      print("three")
      spike1.Color = hit.Color
      spike1.Material = hit.Material
    end
  end
end)

step 4: remove the debugging materials (OPTIONAL)

after fixing and testing, it should work properly right…? right…???

local touchedonce = false
tiospike.SmallSmike.Base.Touched:Connect(hit)
  if (touchedonce == false) then
    if (not hit.Parent:FindFirstChildOfClass("Humanoid")) then
      spike1.Color = hit.Color
      spike1.Material = hit.Material
    end
  end
end)

keeping the debug materials for future changes is optional and not required.


that’s all for the tutorial. thanks for reading and sorry for typing errors.

9 Likes

Although this is a really small and basic tutorial, I believe this would greatly help small Developers facing issues with their codes!

Thank you for sharing this!

5 Likes