Getting Started
This guide will walk you through the basics of Coppermind.
๐ฆ Installationโ
- Manual
- Download the latest release from GitHub
- Add the
srcfolder to your project - Require the module in your code
๐ Defining a Schemaโ
Schemas define the structure and default values for your data. Start by registering one:
local Coppermind = require(path.to.Coppermind)
local PlayerSchema = Coppermind.registerSchema({
name = "PlayerData",
dataTemplate = {
coins = 0,
gems = 0,
inventory = {},
stats = {
level = 1,
xp = 0,
},
},
migrations = {},
})
Schema Propertiesโ
| Property | Type | Description |
|---|---|---|
name | string | Unique identifier for the schema (used as DataStore name) |
dataTemplate | table | Default values for new data |
migrations | {function} | Array of migration functions for schema evolution |
๐ Loading a Storeโ
A store represents a single data entry (e.g., one player's data):
local store = Coppermind.loadStore(PlayerSchema, tostring(player.UserId), {
sessionLocked = true, -- Prevent other servers from loading this data
autoSave = 60, -- Auto-save every 60 seconds
})
Configuration Optionsโ
| Option | Type | Default | Description |
|---|---|---|---|
sessionLocked | boolean? | false | Lock data to this server session |
autoSave | number? | nil | Auto-save interval in seconds |
โณ Waiting for Dataโ
Stores load asynchronously. Wait for the data to be ready:
-- Using the onReady event
store.onReady:Connect(function(store)
print("Data is ready!")
end)
-- Or check the state
if store.state == "READY" then
-- Data is available
end
Store Statesโ
| State | Icon | Description |
|---|---|---|
LOADING | ๐ | Data is being loaded from DataStore |
READY | โ | Data is loaded and available |
SAVING | ๐พ | Data is being saved |
UNLOADING | ๐ค | Store is being unloaded |
UNLOADED | โฌ | Store has been unloaded |
ERROR | โ | An error occurred |
SESSION_LOCKED | ๐ | Another server has locked this data |
๐ Reading Dataโ
Use getData to read the current data:
local data = Coppermind.getData(PlayerSchema, tostring(player.UserId))
if data then
print("Coins:", data.coins)
print("Level:", data.stats.level)
end
Data is Immutable
The data returned by getData is frozen and cannot be modified directly. Use updateData to make changes.
โ๏ธ Updating Dataโ
Use updateData to modify data safely:
- Mutation Style
- Replacement Style
Modify the data directly and return nil:
Coppermind.updateData(PlayerSchema, tostring(player.UserId), function(data)
data.coins += 100
data.stats.xp += 50
table.insert(data.inventory, "sword")
return nil -- Return nil to apply mutations
end)
Return a new data table to replace the entire data:
Coppermind.updateData(PlayerSchema, tostring(player.UserId), function(data)
return {
coins = data.coins + 100,
gems = data.gems,
inventory = data.inventory,
stats = {
level = data.stats.level,
xp = data.stats.xp + 50,
},
}
end)
๐พ Saving Dataโ
- Manual Save
- Auto-Save
Coppermind.saveStore(PlayerSchema, tostring(player.UserId))
Configure auto-save when loading the store:
local store = Coppermind.loadStore(PlayerSchema, key, {
autoSave = 60, -- Save every 60 seconds
})
๐ค Unloading a Storeโ
Always Unload
Always unload stores when they're no longer needed to save data and release session locks!
game.Players.PlayerRemoving:Connect(function(player)
Coppermind.unloadStore(PlayerSchema, tostring(player.UserId))
end)
๐ก Global Eventsโ
Coppermind provides global events for monitoring all stores:
-- Fires when any store is loaded
Coppermind.onLoaded:Connect(function(store)
print("Store loaded:", store.key)
end)
-- Fires when any store is saved
Coppermind.onSaved:Connect(function(store)
print("Store saved:", store.key)
end)
-- Fires when any store encounters an error
Coppermind.onError:Connect(function(store, errorMessage)
warn("Store error:", store.key, errorMessage)
end)
-- Fires when any store is unloaded
Coppermind.onUnloaded:Connect(function(store)
print("Store unloaded:", store.key)
end)