DirectoryWatcher. new Synchronous
Watch a workspace folder and get a signal whenever a file in it is created, edited, or deleted. It is handy for reloading a script or settings file as soon as you save it.
Luau
DirectoryWatcher.new(folder: string) -> DirectoryWatcherParameters
| Parameter | Type | Description |
|---|---|---|
folder | string | Workspace-relative path of an existing folder. |
Returns
DirectoryWatcherA DirectoryWatcher whose OnChanged signal receives the path of each changed file.
Luau
DirectoryWatcher = {OnChanged: Signal}| Field | Type | Description |
|---|---|---|
OnChanged | Signal | Signal fired with the workspace path of each file created, edited, or deleted in the folder. Handlers may yield. |
Usage notes
The folder is checked every 0.1 seconds. Files already present when the watcher is created do not fire; only later changes do. Subfolders are not watched.
Every check reads each file in the folder, so watch a small folder of your own rather than a large one. Call Destroy when you are done.
Example
Luau
makefolder("watched")
local watcher = DirectoryWatcher.new("watched")
watcher.OnChanged:Connect(function(path)
print("Changed:", path)
end)
writefile("watched/settings.json", "{}")
task.wait(1)
watcher:Destroy()
delfolder("watched")