Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Storing a lot of data all at once in a simple manor?

Asked by 5 years ago
Edited 5 years ago

Hey! I'm having trouble with something regarding Data Stores, I know how they work and everything(although confusing sometimes). However, i need to store a large amount of data quickly and easily in a way where i don't have to really change the storage script if i change anything in the values or add any values. I have attempted using tables, and although i am more knowledgeable with tables and arrays, i cant seem to figure this one out. Does anyone have any thoughts of how to store a whole folder worth of data? There is another folder full of values inside the main folder, and those values also have children regarding those values. Thanks! -Manelin

local DSService = game:GetService('DataStoreService'):GetDataStore('MagicalnessTesting') --Change this name to a random name or it will not work

local Variables = {}


game.Players.PlayerAdded:connect(function(plr)
    -- Define variables
    local uniquekey = 'id-'..plr.userId
    local MagicValues = game.ServerStorage:FindFirstChild("MagicValues"):Clone()
    MagicValues.Parent = plr
    print("Put MagicValues in player", plr.MagicValues)
    local MagicSpells = MagicValues.Spells

    wait(2)
    for _, Values in pairs(MagicValues:GetDescendants()) do
        if Values:IsA'StringValue' or Values:IsA'NumberValue' or Values:IsA'BoolValue'then
            table.insert(Variables, Values.Name)
            Variables[Values.Name] = tostring(Values.Value)
            print(Variables[Values.Name].."Vairables")


        end
    end



    -- GetAsync
local GetSaved = DSService:GetAsync(uniquekey)
    if GetSaved then
        for i = 1, #Variables do
        Variables[i].Name = GetSaved[Variables]

        end
    else
        for i = 1, #Variables do
        DSService:SetAsync(uniquekey, Variables)
        end
    end

end)

game.Players.PlayerRemoving:connect(function(plr)
local uniquekey = 'id-'..plr.userId
local MagicValues = plr.MagicValues


local Savetable = {}
for _, Values in pairs(MagicValues:GetDescendants()) do
        if Values:IsA'StringValue' or Values:IsA'NumberValue' or Values:IsA'BoolValue'then
            table.insert(Savetable, Values.Name)
            Savetable[Values.Name] = tostring(Values.Value)

DSService:SetAsync(uniquekey, Savetable)
end
end 
end)
0
Yes, you can store a table of data inside one single DataStore! I'll type something out for you to help. ShinyGriffin 129 — 5y
0
"manor" TheluaBanana 946 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

You can create an entire player's save in a model, like I've done. Whenever you want to add something new, just add another instance value in the model. (Like number value, bool value, stringvalue, etc)

Create the model with everything you want to save and parent it to the script. In my example, i named my model "PlayerSave"

In explorer, it would look like this:

ServerScriptService:
    script
        PlayerSave
            ValueOne
            ValueTwo
            ValueThree
local PlayerSavedData = game:GetService('DataStoreService'):GetDataStore("TEST")

game.Players.PlayerAdded:connect(function(player) --When the player joins
    repeat wait() until script.PlayerSave --wait until the model is actually loaded
    local save = script.PlayerSave:Clone()--Create the PlayerSave Values
    save.Parent = player --Put the model of values inside the player
end)

Then, make the table for everything in your model you made, you will have to add to this table if you added another value instance to your model.

function PlayerDataTable(player) --Name of your table (it can be anything)
    local savevalues = { --in this example, i have 3 values. you can add more, just add to the list when you want.
        ValueOne = player.PlayerSave.ValueOne.Value,
            ValueTwo = player.PlayerSave.ValueTwo.Value,
            ValueThree = player.PlayerSave.ValueThree.Value
    }
    return savevalues
end

Then, you're going to add the player's save onto them when they join.

game.Players.PlayerAdded:connect(function(player)
    repeat wait() until player.Character
    repeat wait() until player:FindFirstChild("PlayerSave") --wait until the player has their save model
    local data = PlayerDataTable(player)
    local playerkey = "player-"..player.userId --the individual player themselves
    local SaveFiles = PlayerSavedData:GetAsync(playerkey) --get the player's save
        if SaveFiles==nil then
            print("player has no save yet") --if the player is new
        else --if the player has a save
            player.PlayerSave.ValueOne.Value = SaveFiles.ValueOne
            player.PlayerSave.ValueTwo.Value = SaveFiles.ValueTwo
            player.PlayerSave.ValueThree.Value = SaveFiles.ValueThree --Puts the save file inside each value. Add to this list when you want to add another value to save.
end)

Saving just works the same way, but add the table.

function SaveData(player)
    local playerkey = "player-"..player.userId --individual user
    local data = PlayerDataTable(player) --the table
    PlayerSavedData:SetAsync(playerkey,data) --save the table to the user
end

Save the entire player's save when they leave the game.

game.Players.PlayerRemoving:Connect(SaveData)
0
This is the entire script, i believe its the simplest way! You only need one script, one datastore key, and one table. ShinyGriffin 129 — 5y
0
If you want to change the values (like for example, you want to change the starting money people have joining the game) you would just go into the model and find the instance and change the value inside the instance. Nothing more! ShinyGriffin 129 — 5y
0
Thanks very much, but what if i wanted to index all the values in a :GetDescendants() loop, to put into an empty table, because the way all the values work, there are too many to throw into an array, wouldnt it be easier to do something like, for _, Values in pairs(Values:GetDescendants()) do? and then index what kind of values will go into the table? and then once those values are found then... Manelin 2 — 5y
0
equal the names of the values that i put into the table to the values? if that makes sense? like... table.insert(Variables, Values.Name)... and then... Variables[Values.Name] = tostring[Values.Value]? Manelin 2 — 5y
View all comments (3 more)
0
the only thing with that that im really confused on, cause so far i have been able to index all the values through the loop, is not only saving it, but equaling all the current values to the saved values(and i am hoping to do that in one line). after find out that there is a save then, Values.Value = GetSaved[Variables[Values.Name]] (GetSaved is what i was using for SavedFiles) Manelin 2 — 5y
0
Thankyou for responding so quickly btw Manelin 2 — 5y
0
I will post up above the script i am using @ShinyGriffin Manelin 2 — 5y
Ad

Answer this question