Data Saving with Roblox DataStores
Persist player data across sessions using DataStoreService, with error handling, session locking, and migration patterns.
Introduction
DataStores are Roblox's cloud database for persisting player progress—coins, inventory, settings, and achievements. Incorrect DataStore usage causes data loss, one of the most devastating bugs in live games. This hub teaches safe patterns with retry logic, session locking, and schema versioning.
Basic Save and Load Pattern
Use DataStoreService:GetDataStore('PlayerData'). On PlayerAdded, load data with pcall and :GetAsync(userId). Store in a server-side table. On PlayerRemoving, save with :SetAsync. Always wrap DataStore calls in pcall—API calls can fail due to rate limits or service issues.
Session Locking and Data Loss Prevention
If a player joins two servers simultaneously, both could load stale data and overwrite each other. Use UpdateAsync with a session lock pattern, or implement a short delay before saving on join. Never save on every small change—batch saves on meaningful events and player leave.
Use Cases
- Currency systems
- Inventory persistence
- Settings and preferences
- Achievement tracking
Best Practices
- Always use pcall around DataStore operations
- Implement retry with exponential backoff
- Version your data schema for future migrations
- Never save tables with Instance references
Troubleshooting
Script runs but nothing happens in-game
Verify the script type matches its location. Server Scripts belong in ServerScriptService; LocalScripts in StarterPlayerScripts or StarterGui. Check the Output window in Studio for error messages.
Changes don't appear after editing
Stop and restart Play mode in Studio. Some scripts cache values on first run. For published games, republish after testing locally.