Skip to main content

Record

Let's say you want to record pyro in your game. Your script might currently look like this:

local pyroFolder = workspace.Fire
local clickDetector = script.Parent.ClickDetector

clickDetector.MouseClick:Connect(function()
for _, part in pyroFolder:GetChildren() do
part.ParticleEmitter.Enabled = true
end
end)

clickDetector.RightMouseClick:Connect(function()
for _, part in pyroFolder:GetChildren() do
part.ParticleEmitter.Enabled = false
end
end)

The first thing we need to do is to include the StacyPilot core module in your script like this:

local core = require(workspace.StacyPilot.Core)

After that you can start recording stuff in your scripts. Remember that StacyPilot shouldn't record results of actions, but their activators. So instead of telling it when every piece of fire went off, we should tell it that we pressed the "On" button. To keep the code clean, let's first move our on/off code to separate functions.

local function turnOn()
for _, part in pyroFolder:GetChildren() do
part.ParticleEmitter.Enabled = true
end
end

local function turnOff()
for _, part in pyroFolder:GetChildren() do
part.ParticleEmitter.Enabled = false
end
end

Now, when the button is pressed, we will turn on the pyro and tell StacyPilot to record it. The core:Record() function needs at least 1 parameter: the name of the module that will be saved. In this case we'll use "Fire" but it could be anything. In a more professional scenario you should probably record all pyro as "Pyro" to improve save/load times.

clickDetector.MouseClick:Connect(function()
turnOn()
core:Record("Fire", "On")
end)

clickDetector.RightMouseClick:Connect(function()
turnOff()
core:Record("Fire", "Off")
end)

Now StacyPilot can record when we activated or deactivated fire, but it can't replay it yet. To do that, we need to run the core:RegisterResponse() function which StacyPilot will run whenever it finds a piece of data from this module. The first parameter needs to be the same as in the :Record function ("Fire" in our case). The data below is all data passed into :Record packed into a table. In out case the first entry is either going to be "On" or "Off"

core:RegisterResponse("Fire", function(data)
local actionName = data[1]

if actionName == "On" then
turnOn()
elseif actionName == "Off" then
turnOff()
end
end)

And that's it! StacyPilot can now record your fire. Full code:

local core = require(workspace.StacyPilot.Core)
local pyroFolder = workspace.Fire
local clickDetector = script.Parent.ClickDetector

local function turnOn()
for _, part in pyroFolder:GetChildren() do
part.ParticleEmitter.Enabled = true
end
end

local function turnOff()
for _, part in pyroFolder:GetChildren() do
part.ParticleEmitter.Enabled = false
end
end

clickDetector.MouseClick:Connect(function()
turnOn()
core:Record("Fire", "On")
end)

clickDetector.RightMouseClick:Connect(function()
turnOff()
core:Record("Fire", "Off")
end)

core:RegisterResponse("Fire", function(data)
local actionName = data[1]

if actionName == "On" then
turnOn()
elseif actionName == "Off" then
turnOff()
end
end)
tip

If you need more examples, you can look at the source code of the official add-ons