Skip to content

Service · inherits Instance

ScriptContext

Official docs ↗

A service that manages the execution of all BaseScript objects.

About ScriptContext

This service controls all BaseScript objects. Most of the properties and methods of this service are locked for internal use.

Methods 3

  • EnableCoverage(instance: Instance) → ()Marks an instance and its descendant scripts for code-coverage tracking. Requires PluginOrOpenCloud security.

    Requires PluginOrOpenCloud security; not callable from game scripts. Registers instance as a coverage root so the engine records per-line and per-function execution coverage for instance and every script descended from it. Retrieve the recorded data with ScriptContext.GetCoverageStats.

  • GetCoverageStats() → ArrayReturns code-coverage statistics for scripts marked by EnableCoverage(). Requires PluginOrOpenCloud security.

    Requires PluginOrOpenCloud security; not callable from experience scripts. Returns an array of coverage results, one entry per tracked script. Each entry is a table with a Script field (the script Instance) and a GetHits() function. Calling GetHits() returns two arrays: per-line hit counts, where -1 marks a line that was never executed or was excluded, and per-function records containing each function's Name, Line, and Hits.

    Like ScriptContext.EnableCoverage, this method raises an error if coverage collection is not enabled.

  • SetTimeout(seconds: double) → ()PluginLimits how long a script is allowed to run without yielding.

    Sets the watchdog time limit that the engine applies to running scripts. The value becomes the per-resumption execution budget: if a thread runs for longer than seconds since it was last resumed without yielding, the watchdog aborts it with the runtime error Script timeout: exhausted allowed execution time.

    The timeout defaults to 0, which disables the watchdog so that scripts may run without any time limit. Call this method with a positive value to enable enforcement, or pass 0 to turn it back off.

Events 1

  • ErrorScriptContext.Error:Connect(function(message, stackTrace, script))Fired when an error occurs.

    Fires when an unhandled error occurs while running a script, reporting the error to any listening code. The engine raises this event from its error-reporting path when a thread terminates with an error, passing the error message, a formatted stackTrace string, and the script that was running.

    This event does not fire for errors raised by the watchdog when a script exceeds its execution-time limit (see ScriptContext.SetTimeout).

ScriptContext also has every member of Instance. Deprecated and Roblox-only members are hidden.

Reference text from the Roblox Creator Documentation, licensed CC BY 4.0, reorganized by rbxtoolshub.com · Studio 0.743.0.7431133.