Basic Script Debugging: Difference between revisions

From Legacy Roblox Wiki
Jump to navigationJump to search
>Camoy
Outdated, uses Source
>JulienDethurens
No edit summary
 
(9 intermediate revisions by 4 users not shown)
Line 1: Line 1:
You've just got you're first script ready and it doesn't work. What now?
You've just got your first script ready and it doesn't work. What now?


== Setup ==
== Setup ==
Line 9: Line 9:
If the script is a Script (rather than LocalScript), you can check for compile errors with "Play" button in studio.
If the script is a Script (rather than LocalScript), you can check for compile errors with "Play" button in studio.


You can also check any script with
You can also check any script by entering this in command bar:
  print( loadstring( ''script''.Source ) )
  print( loadstring( ''script''.Source ) )
For example
For example
Line 49: Line 49:
* Add print calls inside of if, while and other blocks to see whether they are running as expected.
* Add print calls inside of if, while and other blocks to see whether they are running as expected.


[[Category:Pending Removal]]
[[Category:Scripting Tutorials]]

Latest revision as of 04:36, 19 March 2012

You've just got your first script ready and it doesn't work. What now?

Setup

First, make sure you have setup Standard Studio View.

Compile Errors

Next, check your script for compile errors.

If the script is a Script (rather than LocalScript), you can check for compile errors with "Play" button in studio.

You can also check any script by entering this in command bar:

print( loadstring( script.Source ) )

For example

print( loadstring( Workspace.Script.Source ) )

if the script has errors you'll see

nil [string "-- this is the beginning of a sample script with errors..."]:2: 'then' expected near '='

See Common_Scripting_Mistakes for details of error messages.

If there are compile errors, your script will not run at all.

If there are no compile errors you'll see something like

function: 1af01234

where "1af01234" can be any number. Continue to the next section.

Runtime Issues

Assuming your script compiles, there are several ways it could go wrong.

Runtime Errors

If the script encounters a runtime error, it will stop. If the error occurs in an event function, the function may be disconnected, preventing any future event from running.

The easiest way to check for runtime errors is by running in Play Solo mode with output window. If error doesn't occur in Solo, see Debugging for advanced techniques.

If there's no error message, continue to the next section.

Missing Connection

In order for a function to run, it must be called. This means you either need a call

func()

or more likely a connection line

part.Touched:connect( func )

If you do have the connection, add a print to the beginning of the function. Use Play Solo mode if possible to check the print and to see an runtime errors.

Logic Errors

If the connection is working and there's no error in the log, then you are probably looking at a logic error. Adding print calls is the best way to see what's going on inside the function. Again Play Solo mode gives the output for debugging.

Here's some suggestions:

  • Add print calls to print the values of variables used.
  • Add print calls inside of if, while and other blocks to see whether they are running as expected.