How to DataStore multiple BoolValues in a folder

Hey, so basically I made a character shop. The store is working perfectly but I am now struggling to save the bought skins.

Every skin in the game is added in form of a BoolValue to a folder inside a player, if the player has the skin, it will be true and if not, false. This is for the inventory handler to check which values are true and give those skins to the player

image

How can I save all the bool values in the player folder?

Here is my leaderstats code with only the parts for the skin thing and my attempt

local mySkins = Instance.new("Folder" , player)
    mySkins.Name = "mySkins"
    
    for i , v in pairs(game.ReplicatedStorage.Skins:GetChildren()) do -- Here is were the skins in the shop are located
        skin = Instance.new("BoolValue" , mySkins)
        skin.Name = v.Name      
    end
    
    local data

    local success, errormessage = pcall(function()
        data = myDataStore:GetAsync(player.UserId)

    end)

    if success then
        if data then                      
            skin.Value = data.skin
            print("Data Loaded!")
        end        

    else
        print("There was an error while getting your data")
        warn(errormessage)
    end    
       
end)
game.Players.PlayerRemoving:Connect(function(player)

    local data = {        
        skin = player.mySkins:FindFirstChild(skin.Name).Value -- I am aware this is wrong
        
    }
    local success, errormessage = pcall(function()

        myDataStore:SetAsync(player.UserId,data)
    end)    

    if success then
        print("Player Data successfully saved!")
    else
        print("There was an error when saving data")
        warn(errormessage)
    end
end)

I know this is wrong because I am using the FindFirstChild and it is only taking 1st value and it is not working properly, but I tried using GetChildren() or GetDescendants() but couldn’t do it right.

Also not sure if I am overwriting the folder everytime the player joins with the for loop

Any help is really appreciated

4 Likes

I would store into the datastore an array of the names of skins the user has
if the user has the skins: “Cone,” “Rich,” and “Wizard”, you’d do :SetAsync(userid, {"Cone", "Rich", "Wizard"})

you could create the table of names by looping through the folder, mySkins, and inserting the names of the BoolValues that are set to True with table.insert(some table, skin name)

then, when loading the data on join, you’d loop through data from myDataStore:GetAsync(player.UserId) and set the values of the BoolValues that match the names to true

2 Likes

Hey, I am also needing help with something similar to this, I am saving 2 values in the player. It’s not receiving Data.Shirt or .Pants. Obviously this isn’t the whole script, I have all the functions and variables. Just wanted to summarize my problem

-- for when saving
myDataStore:SetAsync(player.UserId, {Shirt, Pants})

-- for when the player joins
local Data = myDataStore:GetAsync(player.UserId)
local Shirt = Data.Shirt
local Pants = Data.Pants

that’s an issue with using dictionaries
shirt would be Data[1]
pants would be Data[2]

if you want to get them with .Shirt and .Pants, then the table you’d save in the datastore should look like

{
   ["Shirt"] = Shirt,
   ["Pants"] = Pants
}

also lol, you should make your own topic in Scripting Support for that <.< but I don’t have a problem with it since it’s a small thing

1 Like