docs/pages/roblox/2-examples.md

87 lines
2.6 KiB
Markdown
Raw Normal View History

2023-07-22 13:25:44 +01:00
<!-- markdownlint-disable MD033 -->
<!-- markdownlint-disable MD026 -->
# Example Scripts
These are a few short examples of things you can do using the built-in `roblox` library.
## Make all parts anchored in a place file
```lua copy
2023-07-22 14:18:03 +01:00
local fs = require("@lune/fs")
2023-07-22 13:25:44 +01:00
local roblox = require("@lune/roblox")
2023-07-22 14:18:03 +01:00
-- Read the place file called myPlaceFile.rbxl into a DataModel variable called "game"
-- This works exactly the same as in Roblox, except "game" does not exist by default.
-- To access "game" you have to load it from a file!
local file = fs.readFile("myPlaceFile.rbxl")
local game = roblox.deserializePlace(file)
2023-07-22 13:25:44 +01:00
local workspace = game:GetService("Workspace")
-- Make all of the parts in the workspace anchored
for _, descendant in workspace:GetDescendants() do
if descendant:IsA("BasePart") then
descendant.Anchored = true
end
end
-- Save the DataModel (game) back to the file that we read it from
2023-07-22 14:18:03 +01:00
file = roblox.serializePlace("myPlaceFile.rbxl")
fs.writeFile(file)
2023-07-22 13:25:44 +01:00
```
2023-07-22 14:18:03 +01:00
---
2023-07-22 13:25:44 +01:00
## Save instances in a place as individual model files
```lua copy
local fs = require("@lune/fs")
2023-07-22 14:18:03 +01:00
local roblox = require("@lune/roblox")
2023-07-22 13:25:44 +01:00
-- Here we load a file just like in the first example
2023-07-22 14:18:03 +01:00
local file = fs.readFile("myPlaceFile.rbxl")
local game = roblox.deserializePlace(file)
2023-07-22 13:25:44 +01:00
local workspace = game:GetService("Workspace")
2023-07-22 14:18:03 +01:00
-- Make sure a directory exists to save our models in
2023-07-22 13:25:44 +01:00
fs.writeDir("models")
-- Then we save all of our instances in Workspace as model files, in our new directory
-- Note that a model file can actually contain several instances at once, so we pass a table here
for _, child in workspace:GetChildren() do
2023-07-22 14:18:03 +01:00
file = roblox.serializeModel({ child })
fs.writeFile("models/" .. child.Name, file)
2023-07-22 13:25:44 +01:00
end
```
2023-07-22 14:18:03 +01:00
---
2023-07-22 13:25:44 +01:00
## Make a new place from scratch
```lua copy
2023-07-22 14:18:03 +01:00
local fs = require("@lune/fs")
2023-07-22 13:25:44 +01:00
local roblox = require("@lune/roblox")
local Instance = roblox.Instance
-- You can even create a new DataModel using Instance.new, which is not normally possible in Roblox
-- This is normal - most instances that are not normally accessible in Roblox can be manipulated using Lune!
local game = Instance.new("DataModel")
local workspace = game:GetService("Workspace")
-- Here we just make a bunch of models with parts in them for demonstration purposes
for i = 1, 50 do
local model = Instance.new("Model")
model.Name = "Model #" .. tostring(i)
model.Parent = workspace
for j = 1, 4 do
local part = Instance.new("Part")
part.Name = "Part #" .. tostring(j)
part.Parent = model
end
end
-- As always, we have to save the DataModel (game) to a file when we're done
2023-07-22 14:18:03 +01:00
local file = roblox.serializePlace(game)
fs.writeFile("myPlaceWithLotsOfModels.rbxl", file)
2023-07-22 13:25:44 +01:00
```