Files and HTTP
Scripts can keep files on disk and talk to web servers. This tutorial saves a script's settings to a file, then fetches data from the web.
The workspace folder
All file functions work inside one folder, Documents\evasive roblox\workspace, next to your scripts folder. Paths are relative to it, and / and \ both work.
| Function | What it does |
|---|---|
writefile(path, text) |
Creates or overwrites a file. Missing folders on the way are made for you. |
appendfile(path, text) |
Adds to the end of a file, or creates it |
readfile(path) |
Returns the whole file. Raises an error if it does not exist. |
isfile(path) / isfolder(path) |
Whether there is a file or folder there |
makefolder(path) |
Creates a folder and any parents |
listfiles(path) |
The files and folders directly inside, sorted. listfiles("") lists the workspace itself. |
delfile(path) |
Deletes a file or an empty folder. true if something was deleted. |
delfolder(path) |
Deletes a folder and everything in it |
The functions that touch a file's contents work with seven kinds of file: .txt, .lua, .luau, .json, .cfg, .dat and .ini. Any other name raises "file extension not allowed". Files are read and written byte for byte, so binary data is fine. Keep it under .dat.
A path cannot leave the workspace. An absolute path or .. that climbs out raises an error.
Saving settings
JSON is the easy way to store a table. HttpService turns tables into JSON text and back:
local HttpService = game:GetService("HttpService")
local FILE = "my-script/settings.json"
local defaults = { color = { 255, 80, 80 }, range = 500, names = true }
local function loadSettings()
if not isfile(FILE) then
return table.clone(defaults)
end
local ok, data = pcall(function()
return HttpService:JSONDecode(readfile(FILE))
end)
if not ok or type(data) ~= "table" then
warn("settings file is broken, using defaults")
return table.clone(defaults)
end
for key, value in pairs(defaults) do
if data[key] == nil then
data[key] = value
end
end
return data
end
local function saveSettings(settings)
writefile(FILE, HttpService:JSONEncode(settings))
end
local settings = loadSettings()
print("range is", settings.range)
settings.range += 100
saveSettings(settings)Run it a few times and the range keeps growing, because each run reads what the last one saved.
If your script has a menu tab, you may not need this at all. Widget values are saved for you (see Making your own menu tab).
Logging
appendfile is handy for a log you can read later:
local function log(...)
local parts = {}
for i = 1, select("#", ...) do
parts[#parts + 1] = tostring((select(i, ...)))
end
appendfile("logs/session.txt", os.date("%H:%M:%S ") .. table.concat(parts, " ") .. "\n")
end
log("joined place", game.PlaceId)Fetching from the web
httpget(url) returns the body of the page as a string:
local HttpService = game:GetService("HttpService")
local body = httpget("https://games.roblox.com/v1/games?universeIds=" .. game.GameId)
if body == "" then
warn("request failed")
return
end
local data = HttpService:JSONDecode(body)
local info = data.data and data.data[1]
if info then
print(info.name, "has", info.playing, "players online")
endhttppost(url, body, contentType, headers) sends data. The content type defaults to "application/json":
local HttpService = game:GetService("HttpService")
local reply = httppost(
"https://httpbin.org/post",
HttpService:JSONEncode({ place = game.PlaceId }),
"application/json",
{ ["X-Example"] = "evasive" }
)
print(reply)Both also take a table of headers (the second argument of httpget). game:HttpGet(url) and game:HttpPost(...) are the same functions under their Roblox names.
A few things to know:
- Your thread pauses while the request is in flight. Other scripts and your other threads keep running.
- A request that fails to connect returns
"". An error page (404, 500) returns whatever the server sent. - A request gives up after 30 seconds.
- Each script can have up to 8 requests in flight. Extra ones wait their turn.
- Stopping the script cancels its requests.
JSON in short
| Call | Result |
|---|---|
HttpService:JSONEncode(value) |
a JSON string |
HttpService:JSONDecode(text) |
a table (or a number, string or boolean) |
HttpService:GenerateGUID() |
a random id like "{1C3A...}" |
Base64
base64encode(text) and base64decode(text) convert to and from Base64. They handle any bytes, including zero bytes.