Skip to main content

Getting Started

This guide will walk you through the basics of Coppermind.


๐Ÿ“ฆ Installationโ€‹

  1. Download the latest release from GitHub
  2. Add the src folder to your project
  3. 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โ€‹

PropertyTypeDescription
namestringUnique identifier for the schema (used as DataStore name)
dataTemplatetableDefault 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โ€‹

OptionTypeDefaultDescription
sessionLockedboolean?falseLock data to this server session
autoSavenumber?nilAuto-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โ€‹

StateIconDescription
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:

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)

๐Ÿ’พ Saving Dataโ€‹

Coppermind.saveStore(PlayerSchema, tostring(player.UserId))

๐Ÿ“ค 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)

๐ŸŽฏ Next Stepsโ€‹

GuideDescription
๐Ÿ“‹ SchemasData templates and type safety
๐Ÿ“‚ StoresSession locking and store management
โœ๏ธ Data OperationsReading and updating data
๐Ÿ”„ MigrationsEvolving your data schema
๐Ÿ’ฑ TransactionsAtomic operations across stores
๐Ÿงช TestingMock mode and testing patterns