writefile
Writes data to a file.
Syntax
writefile(path: string, data: string) -> voidParameters
| Parameter | Type | Description |
|---|---|---|
path | string | The file path |
data | string | The data to write |
Returns
This function does not return a value.
Description
writefile writes data to a file, creating the file if it doesn't exist or overwriting it if it does. Parent folders are created automatically.
Example
-- Write a simple text file
writefile("hello.txt", "Hello, World!")
-- Verify the content
print(readfile("hello.txt")) -- "Hello, World!"Writing JSON
local HttpService = game:GetService("HttpService")
local settings = {
volume = 0.5,
graphics = "high",
keybinds = {
toggle = "F1",
menu = "F2"
}
}
writefile("settings.json", HttpService:JSONEncode(settings))Writing to Subfolders
-- Parent folders are created automatically
writefile("data/saves/slot1.txt", "Save data here")
-- Or create them explicitly
makefolder("logs")
writefile("logs/session.log", "Session started")Binary Data
-- Write binary data (as string)
local binary = string.char(0, 1, 2, 3, 255)
writefile("binary.bin", binary)Related Functions
readfile- Read a fileappendfile- Append to a fileisfile- Check if file exists