SequenceGenerator Module Instruction Manual
Overview:
Module - https://create.roblox.com/store/asset/120318033862334/SequenceGenerator?viewFromStudio=true&keyword=&searchId=9065f062-2f78-49c7-8c82-72078afd2e18
The SequenceGenerator
module is a versatile tool for generating sequences of items based on a provided configuration. It ensures that each sequence contains the specified items in their exact quantities, but with the order of items randomized. This module is suitable for various applications where you need a predictable set of elements in a random order for each series.
Module Code (SequenceGeneratorModule):
local SequenceGenerator = {}
SequenceGenerator.__index = SequenceGenerator
function SequenceGenerator.new(config, seed)
--
--Creates a new sequence generator.
-- Args:
-- config (table): A dictionary specifying the items and their quantities. Example:
-- {item1 = 2, item2 = 1}
--seed (number, optional): A seed value for the random number generator. Defaults to nil.
--
local self = setmetatable({}, SequenceGenerator)
self.config = config
if seed then
math.randomseed(seed)
end
self.currentSeries = {} -- Current sequence of items
self.onItemAvailable = Instance.new("BindableEvent") -- Event when an item is available
self.onSeriesEnd = Instance.new("BindableEvent") -- Event when a sequence is finished
self.currentMoveIndex = 0 -- Index of the current move in the sequence
self.isGenerating = false
return self
end
function SequenceGenerator:_createSeries()
--Creates a sequence of items in a random order, preserving their quantities.
local series = {}
-- Add items to the sequence based on their specified quantities
for item, count in pairs(self.config) do
for _ = 1, count do
table.insert(series, item)
end
end
-- Shuffle the items in the sequence randomly
for i = #series, 2, -1 do
local j = math.random(i)
series[i], series[j] = series[j], series[i]
end
return series
end
function SequenceGenerator:generateSeries()
if self.isGenerating then return end
self.isGenerating = true
self.currentSeries = self:_createSeries()
self.currentMoveIndex = 0
return self.currentSeries
end
function SequenceGenerator:playNextMove()
if not self.isGenerating then return end
-- Check if a sequence is available
if not self.currentSeries or #self.currentSeries == 0 then
self.isGenerating = false
return
end
-- If the sequence is finished
if self.currentMoveIndex >= #self.currentSeries then
self.onSeriesEnd:Fire()
self.isGenerating = false
return
end
local item = self.currentSeries[self.currentMoveIndex + 1]
self.onItemAvailable:Fire(item)
self.currentMoveIndex += 1
end
function SequenceGenerator:stopGeneration()
self.isGenerating = false
end
function SequenceGenerator:getAvailableItems()
return self.currentSeries
end
return SequenceGenerator
How to Use:
-
Require the Module: In your main script, import the
SequenceGeneratorModule
usingrequire()
:
local SequenceGeneratorModule = require(game.ServerScriptService.SequenceGeneratorModule)
-
Create a Generator Instance: Create an instance of the
SequenceGenerator
by calling thenew()
method. You’ll need to provide:
-
config
(table): This table defines the items to be generated and their respective quantities. For example:{item1 = 2, item2 = 1, item3 = 3}
. -
seed
(number, optional): A seed value for the random number generator. If you provide a seed, the same sequences will be generated every time you run the game, which is useful for testing.
local config = {item1 = 2, item2 = 1, item3 = 3}
local seed = 42 -- Optional seed value
local sequenceGen = SequenceGeneratorModule.new(config, seed)
-
Set up Event Listeners: You can use the
onItemAvailable
andonSeriesEnd
events to handle each generated item and the end of a sequence respectively:
sequenceGen.onItemAvailable.Event:Connect(function(item)
print("Item: " .. item)
end)
sequenceGen.onSeriesEnd.Event:Connect(function()
print("Sequence finished")
end)
-
Generate a Sequence: Call the
generateSeries()
method to create a new sequence.
local series = sequenceGen:generateSeries()
-
Play the Sequence: Use the
playNextMove()
method to retrieve the next item in the sequence, one at a time. Call this as many times as you need, or in a loop.
-- Manually play a series of moves
for _ = 1, 100 do -- Example with up to 100 moves
if sequenceGen.isGenerating then
sequenceGen:playNextMove()
wait(0.2)
else
print("Sequence finished, items ended")
break
end
end
- Access available Items: To get the current list of items from the current serie, use:
-- Example how to access available items
local allItems = sequenceGen:getAvailableItems()
print("Available items:", table.concat(allItems, ", "))
-
Stop generation You can also use method
stopGeneration
, if you need to stop generator from producing new items.
sequenceGen:stopGeneration()
Key Methods:
-
SequenceGenerator.new(config, seed)
: Creates a newSequenceGenerator
instance. -
generateSeries()
: Creates a new series of items based on the providedconfig
. -
playNextMove()
: Plays the next move (item) from the series, and fires theonItemAvailable
event with item. When series is finished fires theonSeriesEnd
. -
stopGeneration()
: SetsisGenerating
flag tofalse
. -
getAvailableItems()
: Returns current list of all generated items in current series.
Configuration (config
Table):
- The
config
table is a dictionary where keys are the names of items, and values are their quantities.- Example:
{item1 = 2, item2 = 1, item3 = 3}
- Example:
Events:
-
onItemAvailable
: Fires every time an item is ready to be used in your game. The callback function takes theitem
value as an argument. -
onSeriesEnd
: Fires when the current series has been fully used (all items have been played).
Customization Options:
-
Item Configuration (
config
): Change theconfig
table to adjust which items are generated and how many of each you want. -
Seed Value (
seed
): If you need the sequences to be the same every time, use a consistentseed
value. If you want them to be randomized, you can skip theseed
parameter. -
Number of Series: You can call
generateSeries
multiple times to generate multiple new series.
Example Script:
local SequenceGeneratorModule = require(game.ServerScriptService.SequenceGeneratorModule)
local config = {item1 = 2, item2 = 1, item3 = 3}
local seed = 42
local sequenceGen = SequenceGeneratorModule.new(config, seed)
sequenceGen.onItemAvailable.Event:Connect(function(item)
print("Item: " .. item)
end)
sequenceGen.onSeriesEnd.Event:Connect(function()
print("Series finished")
end)
-- Generate and play a sequence manually
local series = sequenceGen:generateSeries()
for _ = 1, 100 do
if sequenceGen.isGenerating then
sequenceGen:playNextMove()
wait(0.2)
else
print("Series finished, items ended")
break;
end
end
-- example how to access available items
local allItems = sequenceGen:getAvailableItems()
print("Available items:", table.concat(allItems, ", "))
sequenceGen:stopGeneration()
This manual should provide you with a good understanding of how to use the SequenceGenerator
module in your Roblox games. If you have further questions, feel free to ask!