DataStore 5 min read

Save Player Preferences and Build a Music Toggle

Separate the local audio control from persistent server controlled settings and protect existing preferences on load failure.

Start with a local control

Place LobbyMusic under SoundService. A LocalScript can mute its volume and restore the original value when clicked again. Avoid restarting the audio track just to change volume. Keep soundtrack and effects controls separate when they serve different needs.

Save a boolean on the server

Use a published test place with its own data store name. Load the preference with a protected call and record whether it succeeded. Save only after a successful load. This prevents a temporary failure from overwriting a value the player previously chose. Update the server attribute through a validated control when wiring the two examples together.

Verify load and save behavior

Turn music off, leave, and reconnect to the test place. Check that the stored choice restores. Then simulate a failing load with a test stub and confirm that the save routine skips that user.

Build the local soundtrack switch first

Insert a Sound called LobbyMusic in SoundService and configure it with an asset you can use in your place. Add a TextButton with the linked LocalScript and test the toggle while the track is already playing. Changing volume lets the music continue from its current point when unmuted. Keep music and sound effects separate so a player can turn down one without losing feedback from pickups or hazards. Give the button a label that reflects the current state after each input. For the first test, configure the track with a known nonzero original volume. Verify that mute changes that property before bringing a server preference into the setup.

Define the saved value and its owner

Use a boolean such as MusicEnabled. Pick one default value and decide what it means when a player's key has never been written. The server should load the value and own the copy that gets saved. The client can read an attribute and apply its local music behavior. When the setting is changed, update the UI and send only the new boolean to a server handler that acts on the requesting player. Validate its type at that boundary. The client should never select another player's data store key. Pick a stable store name for this setting so changing the button's display text later does not affect existing player data.

Test in a separate published place

Studio can only exercise real data store access when you have configured the appropriate testing settings for a published place. Use a separate test version and a distinct store name so the experiment cannot overwrite a live preference. Wrap network operations in protected calls. On a successful read of a missing key, use the default and allow a later save. On a failed read, the server can put a temporary setting on the player for the current visit, but should remember that the data wasn't loaded. Skipping the write in that case protects an existing preference. In addition to testing a normal visit, test that path with a stub or a deliberately unavailable test configuration.

local ok, stored = pcall(store.GetAsync, store, key)
if ok then
    player:SetAttribute("MusicEnabled", stored ~= false)
end

Save only an eligible session

The linked example saves on player departure or while the server is closing. Only write a preference after a successful load. This is a lightweight pattern for a single boolean, where a failed save can be logged without blocking gameplay. Keep track of which sessions loaded correctly and remove that state after the player leaves. If you extend the pattern to many settings, store a versioned table and define how older records receive defaults without losing values they already had. Avoid a write for every click if players can switch rapidly. Capture the final value or use a short save delay for repeated changes. Keep your error handling explicit enough that you can distinguish a failed read from a failed write.

Verify the full preference loop

Enter the test place, turn the music off and leave. Join again and verify both that the track is quiet and that the button label shows the stored preference. Turn it back on and repeat. If the correct attribute appears but the sound does not follow it, look at the local binding rather than the data store. If no value survives a successful visit, inspect Output around the write. Use two test accounts or fixtures to confirm their stored values are separate. Finally, simulate an unsuccessful load and verify that the departure handler does not write a default. This is the failure case that can silently erase a setting if the two situations are treated alike.

Related scripts