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

Is it possible to store a list of values in a datastore?

Asked by 3 years ago

For example, the players in my game have a "HouseType" value which is an integer value. If this is changed, I want to keep track of their previously owned houses so they can change back to an older house. Any suggestions on how to implement this?

1 answer

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

Hey pickles,

You can save multiple values using tables and saving those into your datastore. You can keep a whole history of your houses and simply get the last saved value to get their currently owned house.

It probably looks something like this:

Rough Draft

Loading Data

local ds = game:GetService("DataStoreService");
local houses = ds:GetDataStore("Houses");
local plrs = game:GetService("Players");

plrs.PlayerAdded:Connect(function(plr) --Loading the data
    local key = plr.UserId;
    local success, house_types;
    local first_try = true;
    while not success do

        if not first_try then
            wait(7) -- waits 7 seconds because of DataStore limitations.
        end

        first_try = false;
        success, house_types = pcall(
            houses.GetAsync, 
            houses, 
            key
        );
    end

    local current_house_type;

    if house_types then -- If there is any saved data.
        current_house_type = house_types[#house_types];
    else
        current_house_type = "none"; -- The default house type if the player doesn't have any saved data.
    end

    -- And then you would set this current_house_type variable to some value in the game.
end)

Saving Data

plrs.PlayerRemoving:Connect(function(plr) -- Saving data
    local key = plr.UserId;
    local current_house_type; -- You will get this from however you are keeping track of it in-game. It could be from a value inside the game or from a script somewhere... This will be the value to save.
    local success, response;
    local first_try = true;

    while not success do
        if not first_try then
            wait(7);
        end
        first_try = false;
        success, response = pcall(
            houses.SetAsync,
            houses,
            key,
            current_house_type or "none" -- ternary operator, basically the current_house_type or "none" will be saved.
        )
    end
end)

Keep in mind this is just a rough draft so you will need to make some changes and modify it to suit your game. I haven't even scripted the part where there is only one player in the game and the server closes right as the player leaves. For that, look at game:BindToClose() which you can study more in detail here.

Check these out if you're confused about anything in this script:

Pcalls

Datastore Service

Well, good luck!

~ Best regards,

~ Idealist Developer

0
Im getting flagged for useless characters when im trying to ask a question plz help Il_5x5 10 — 3y
0
Thank you so much, this helps me a lot! pickles980 4 — 3y
Ad

Answer this question