Skip to main content

Defer

Creates a temporary scope that cleans up automatically after the callback executes.

const result = vault.Defer((scope) => {
const part = new Instance("Part")
scope.Add(part) // Will be cleaned after callback

const connection = scope.Connect(part.Touched, () => {
print("Touched")
})

return "some result"
})

Parameters

ParameterTypeDescription
callbackVaultDeferCallbackFunction that receives a temporary scope vault

Returns

Cleanable \| undefined - The callback's return value, or undefined if error

Behavior

  • Creates a child vault and passes it to the callback
  • All resources added to the scope are automatically cleaned after callback
  • If callback errors, logs the error and returns undefined
  • Perfect for temporary operations that need cleanup