DirectoryWatcher.new(path?, recursive?)
Creates and starts a watcher; OnChanged fires for every change kind and Destroy stops it.
Syntax
DirectoryWatcher.new(path: string?, recursive: boolean?) -> DirectoryWatcher
Parameters
| Parameter | Type | Description |
|---|---|---|
path | string? | Workspace-relative folder. Defaults to the workspace root. |
recursive | boolean? | Watch subfolders too. |
Returns
| Type | Description |
|---|---|
DirectoryWatcher | Running watcher object. |
Description
Logic change: new() now auto-starts the watcher (the folder is opened and a monitor thread queues changes immediately). Calling start() again is a no-op while running.
Logic change: events are dispatched from the executor scheduler tick instead of a Lua pump thread. OnChanged now receives created, modified, deleted and renamed events (previously only modified), along with the workspace-relative path.
The metatable is registered as DirectoryWatcher.Metatable so property access works, On* properties return event objects with :Connect, and watchers expose Destroy().
Example
local watcher = DirectoryWatcher.new("scripts")
watcher.OnChanged:Connect(function(path)
print("changed", path)
end)
writefile("scripts/new.lua", "print(1)")
Notes
- Destroy stops the monitor thread and closes the directory handle.
- Direct setters such as DirectoryWatcher.OnChanged(watcher, callback) still work.