Hello, I am trying to build a table that essentially builds itself through looping throughout names in the table and if it cannot find the name then it inserts a name with a bool value (i forgot the name for what this is called) and then the datastore server script saves it when you leave so that the player can keep the favorited sounds, right now I am not sure how you would save or change a table that’s in a datastore without manually adding (names with a bool value) in it.
I’ve tried to search for answers but they are very lack luster and do not cater to what I am trying to find.
local dataStoreService = game:GetService("DataStoreService")
local DS_ = dataStoreService:GetDataStore("FavoriteDataStore")
local players = game:GetService("Players")
players.PlayerAdded:Connect(function(player: Player)
local data = {}
local success, errorMessage = pcall(function()
data = DS_:GetAsync(player.UserId)
end)
table.insert(data, "sound" = false)
end)
you dont do this when you want to add a key to a dictionary table because that just adds a direct value without a name at the end of the list in the data table
to add a key with a value in a table you do this:
data["sound"] = false
if the key "sound" already exists that will modify the existing key’s value to false
local Ok, Err = pcall(function()
data = DS_:GetAsync(player.UserId)
end)
if Ok then
local soundEnabled = data["sound"] --> You could also use data.sound
end
The issue here is that table.insert() doesn’t work the way you’re using it. You’re trying to use it like a dictionary assignment, but table.insert() adds values sequentially by index. What you actually need is a dictionary-style table where keys are sound names and values are booleans.Here’s the corrected approach:Loading and initializing the table:lualocal dataStoreService = game:GetService("DataStoreService")local DS_ = dataStoreService:GetDataStore("FavoriteDataStore")local players = game:GetService("Players")players.PlayerAdded:Connect(function(player: Player) local data = {} local success, errorMessage = pcall(function() data = DS_:GetAsync(player.UserId) end) -- If no data exists, create an empty table if not data then data = {} end -- Store in player attribute or elsewhere accessible player:SetAttribute("FavoriteSounds", data)end)Adding/updating sounds:lualocal function toggleFavorite(player: Player, soundName: string) local favorites = player:GetAttribute("FavoriteSounds") or {} favorites[soundName] = not (favorites[soundName] or false) player:SetAttribute("FavoriteSounds", favorites)endSaving on player leave:luaplayers.PlayerRemoving:Connect(function(player: Player) local favorites = player:GetAttribute("FavoriteSounds") or {} local success, errorMessage = pcall(function() DS_:SetAsync(player.UserId, favorites) end) if not success then warn("Failed to save favorites:", errorMessage) endend)Key points:- Use dictionary tables ({soundName = true}) instead of table.insert() for key-value pairs- Always check if loaded data is nil before using it- Use pcall() for both GetAsync and SetAsync to handle potential errors- Consider adding a version number in your saved data to handle future schema changesThis pattern scales well and is the standard for player progression data in Roblox games.