readfile
Reads the contents of a file.
Syntax
readfile(path: string) -> stringParameters
| Parameter | Type | Description |
|---|---|---|
path | string | The file path |
Returns
| Type | Description |
|---|---|
string | The file contents |
Description
readfile reads and returns the entire contents of a file as a string.
Example
-- Write and then read a file
writefile("message.txt", "Hello, World!")
local content = readfile("message.txt")
print(content) -- "Hello, World!"Reading JSON
local HttpService = game:GetService("HttpService")
-- Write JSON data
local data = {name = "Player", score = 100}
writefile("save.json", HttpService:JSONEncode(data))
-- Read and parse JSON
local content = readfile("save.json")
local loaded = HttpService:JSONDecode(content)
print(loaded.name, loaded.score) -- "Player" 100Error Handling
local function safeRead(path)
local success, result = pcall(readfile, path)
if success then
return result
else
warn("Failed to read:", result)
return nil
end
end