GlobalStockService
A Roblox module for managing global stock across servers using DataStoreService and MemoryStoreService. Supports forced stock overrides, automatic restocking, random stock prediction based on a global key, callbacks for stock changes
The GlobalStockService module is NOT related to financial stocks or the stock market.
link: GitHub - V1nyI/roblox-GlobalStockService: A Roblox module for managing **global synchronized stocks** like shop restocks, timed events, and forced overrides across all servers
https://create.roblox.com/store/asset/102142312665442/GlobalStockService
Features
- global stock generation with a shared global key
- Automatic periodic restocking with configurable intervals
- Forced stock overrides with expiration via MemoryStoreService
- Callbacks for stock changes and forced stock updates
- Safe update retries and key rotation support
- Debug logging and version update notifications
- Day-of-week and date range restrictions for stock availability
Update logs, Version “v1.0.8”
- Stock types (current types: “normal”, “datelimited”, “dayofweeklimited”)
- Date range restrictions
- Day-of-week restrictions
Example usage:
local GlobalStockService = require(path.to.GlobalStockService)
--// Example 1: Normal Stock
GlobalStockService.CreateStock(
"NormalStock", -- stock name
{
{name = "Apple", chance = 80, minAmount = 1, maxAmount = 5},
{name = "Banana", chance = 50, minAmount = 1, maxAmount = 3},
}, -- items
1, -- minItems
2, -- maxItems
60 -- restockInterval in seconds
)
--// Example 2: DateLimited Stock
GlobalStockService.CreateStock(
"HolidayStock",
{
{name = "CandyCane", chance = 100, minAmount = 1, maxAmount = 2},
{name = "GiftBox", chance = 60, minAmount = 1, maxAmount = 1},
},
1, -- minItems
2, -- maxItems
200, -- restockInterval
"DateLimited", -- type
{
start = {year = 2025, month = 12, day = 23},
["end"] = {year = 2025, month = 12, day = 31}
} -- date range
)
--// Example 3: DayOfWeekLimited Stock
GlobalStockService.CreateStock(
"WeekendStock",
{
{name = "Chocolate", chance = 90, minAmount = 1, maxAmount = 4},
{name = "Juice", chance = 70, minAmount = 1, maxAmount = 2},
},
1, -- minItems
2, -- maxItems
5, -- restockInterval
"DayOfWeekLimited", -- type
{
days = {"Thursday", "Sunday"}
} -- days of the week
)
-- Callback to see when stocks change
GlobalStockService.OnStockChanged(function(stockName, oldStock, newStock, time)
print("Stock changed:", stockName)
print("Old stock:", oldStock)
print("New stock:", newStock)
print("Time:", time)
end)
- Useful
- Neutral
- Confusing
- Don’t like it
Installation
- Copy
GlobalStockService.luainto your Roblox project (Only inServerScriptService) do not expose this module to any client. - Require it in your script:
local GlobalStockService = require(path.to.GlobalStockService)
Basic Usage
Create a stock configuration and start its update loop:
local stockItems = {
{name = "ItemA", chance = 50, minAmount = 1, maxAmount = 3},
{name = "ItemB", chance = 30, minAmount = 2, maxAmount = 5},
{name = "ItemC", chance = 80, minAmount = 1, maxAmount = 1},
}
local myStock = GlobalStockService.CreateStock("MyShopStock", stockItems, 1, 3, 600)
local currentStock = GlobalStockService.GetCurrentStock("MyShopStock")
for _, item in ipairs(currentStock) do
print(item.name, item.amount)
end
Forced Stock Overrides
Force the next stock to a specific list for a defined number of restocks:
local forcedStock = {
{name = "SpecialItem", amount = 10}
}
GlobalStockService.ForceNextStock("MyShopStock", forcedStock, 2)
-- Clear forced stock override:
GlobalStockService.ClearForcedStock("MyShopStock")
Event Callbacks
Subscribe to stock change events:
GlobalStockService.OnStockChanged(function(stockName, oldStock, newStock, restockTime)
print("Stock changed for", stockName)
end)
Subscribe to forced stock change events:
GlobalStockService.OnStockForceChanged(function(stockName, oldStock, newStock, timer)
print("Forced stock changed for", stockName)
end)
Subscribe to forced stock expiration events:
GlobalStockService.OnForcedStockExpired(function(stockName)
print("Forced stock expired for", stockName)
end)
Advanced Usage
Rotate the global key manually
local success, newKeyOrError = GlobalStockService.ForceRotateGlobalKey()
if success then
print("Global key rotated successfully")
else
warn("Failed to rotate global key:", newKeyOrError)
end
Enable or disable debug logging
GlobalStockService.SetDebug(true) -- Enable debug logs
GlobalStockService.SetDebug(false) -- Disable debug logs
API Overview
- CreateStock(name, items, min, max, interval) - Create and start a new stock configuration
- GetCurrentStock(name) - Get current stock for a stock name
- ForceNextStock(name, list, restocks) - Force next stock list for a given number of restocks
- ClearForcedStock(name) - Clear forced stock override
- OnStockChanged(callback) - Subscribe to normal stock change events
- OnStockForceChanged(callback) - Subscribe to forced stock change events
- OnForcedStockExpired(callback) - Subscribe to forced stock expiration events
- ForceRotateGlobalKey() - Manually rotate the global key
- StopStock(name) - Stop the stock update loop for a stock
- SetDebug(enabled) - Enable or disable debug logging
License
MIT - see LICENSE for details.
GitHub: V1nyI
- Yes
- Maybe
- No
