Errors and debugging
Inspect script errors, handle expected failures, and diagnose missing instances.
An unhandled script error reports a message and source line, then stops the current thread. Other scripts and event connections continue running.
Inspect values
Use print for diagnostics and warn for warning output.
GetFullName identifies an instance by its hierarchy path.
print(script:GetFullName())
print(workspace:GetChildren())Handle an expected failure
pcall returns a success flag followed by return values or an error. Handle the
failure explicitly. Do not discard the error without reporting or recovery.
local ok, result = pcall(function()
return game:GetService("StorageService"):Open("scores"):Get("player-42")
end)
if not ok then
warn(result)
endUse assert to check a precondition and error to raise an error explicitly.
Attempt to index nil
This error occurs when code indexes a value that is nil. A missing child is one
possible cause. FindFirstChild returns nil when no matching child exists.
local door = workspace:FindFirstChild("Door")
if door then
print(door.Position)
endCheck the following conditions beside the failing lookup:
- The name and parent match the actual hierarchy.
- A required replicated child has arrived. Use
WaitForChildwith a timeout on clients. - The instance is available on the current execution side.
ServerStorageis not replicated to clients. - An optional reference exists.
Players.LocalPlayerisnilon the server, andPlayer.Charactercan benilbetween spawns.
Script does not execute
Check Disabled and the execution container. A Script in StarterGui does not
execute. A LocalScript in workspace does not execute. Add a diagnostic at the
start of the script to confirm execution.
Handler executes repeatedly
Touched reports part contacts. Multiple limbs and repeated contacts can produce
several events for one character. Use a cooldown or guard for actions that must
run once.
Each Connect call creates a connection. Reconnecting in loops or other handlers
can register duplicate callbacks. Disconnect the previous connection before
replacing it.
Instance is not visible
Check Parent, visibility properties, position, and execution side. An
unanchored part can fall below FallenPartsDestroyHeight and be removed. Client
changes may be visible only to that client.