Debugging, a simple tutorial on debugging your scripts.
Did you any time have issues with your code? Having a error that brought you to your nerves? Or something similar to so? Well, then this tutorial might be 2U!
How would you debug, without having any issues with the code? I’d believe that’s impossible. So, to find the issue, we can do many things. Such as, print debugging, looking for errors in the output, revising your code. And of course, using the debugging tools. (Even though I did not have a look at them, there is a article on them at the devhub! I’d also consider myself doing bad practice, since, I never used, and won’t be explaining anything about Studio’s debugging tools…)
There are multiple errors you can get, but, I’ll only explain some of them, and then later, you know how to fix more advanced issues. Or, even if you don’t, it would be very good for you to search, and if you don’t find any source or potential solution, make a topic here.
script.Parent.Touched:Connect(function(hit) local plr = hit['OMG HEAVENS DOOR!!!!!!!'] print(plr) end)
Now, would you think this works? Or not? Well, let’s take a look at it.; Firstly, we make an anonymous function (Which can’t be called any time later), attached to a .Touched event, having a parameter of called ‘hit’, which is going to be the part which touched. Until now, we had no issues whatsoever. But it’s now, where things start getting bad. We define a variable called ‘plr’, which receives the value of a object, which is
hit['OMG HEAVENS DOOR!!!!!'], though, no time before we did create a instance called ‘OMG HEAVENS DOOR!!!’ inside the part which touched
script.Parent, and we would receive an error, and that error would be:
OMG HEAVENS DOOR!!!!!!! is not a valid member of Part "(Index)", now, just by looking at it, we can see everything. The issue is that ‘OMG HEAVENS DOOR!!!’ is not a thing. And, it wouldn’t print anything, because once the error happens, all following code just gets eliminated, as if you placed a
return in the middle of your script. For my case, there wouldn’t be any solution, since it’s just a example, but, if it were to happen to you, it could be because of a typo, the instance not being there before the check, or, just… It being non-existent.
Code Sample 2:
script.Parent.Touched:Connect(function(hit) local plr local character = plr.Character end)
Did you catch the error yet? If not, let me explain. Basically, we have the same scenerio of the last code sample, the only difference is the code being ran inside the function. The error being thrown would be:
ScriptLocation:Line: attempt to index nil with 'Character'. Think of ‘ScriptLocation’, as where the script which gave the error was indexed in the hierarchy, and think of ‘Line’, as the line which thrown the error. Basically, we tried to index ‘Character’, on something which had no value, this was, ‘plr’. Though, of course, I let it be blank, but, in your case, it could be something else, and, without even having your knowledge, it gave a error. To fix this, we would have to give plr a instance value, and be sure ‘character’ is inside ‘plr’, or else, it would give the same error the last code sample gave.
Code Sample 3:
script.Parent.Touched:Connect(function(hit) local plr = game.Players:GetPlayerFromCharacter(hit.Parent) if plr then local char = plr.Character or plr.CharacterAdded:Wait() char.HumanoidRootPart.Position = 'k' end end)
Any thoughts? If none, and you have no idea what this does, then let me explain further. As of the past scenarios, we attach a .Touched event to a anonymous function (Functions which cannot be later addressed/run). We then create a variable called ‘plr’, which receives a instance value, which is gonna be
game.Players:GetPlayerFromCharacter(hit.Parent). Though, you should now say ‘Oh, but what if a part hits it, and it’s not a child of a player’s character?’, Well, what we do is we check if plr is not nil, by just doing
if plr then, without even have to use
if plr ~= nil then. Then later, we address the character of the player, and we try to set the Position of the HumanoidRootPart to
'k'. The error given would be: ScriptLocation:Line: invalid argument #3 (Vector3 expected, got string). Basically, what happened, is that we tried to assign a value, to a value which wasn’t the correct one. It’s as if someone asked you, ‘What’s 2+2?’, and you said ‘y e s’, which wasn’t what the one who asked was expecting. Same thing happens here, we would need a Vector3 value. The solution, of course, would be to give a Vector3 value, or, at least, the correct value.
22:40:15.226 - Stack Begin
Sorry if the post seems to, or even, IS, not good for #resources:community-tutorials, I’m trying to help people so they don’t get stuck or anything, if you have any concerns, leave a reply, or PM me. Also sorry for not listing too many things, there are a LOT of errors you can get, and I probably wouldn’t be able to list them all here.