Need help with DataStore

Hello!

So i’m currently in need of help with my DataStore script.


What I want to achieve

So basically, I want to load a value. Like this :

But!
not the value of it. I want to save the actual IntValue.

How could I code this?

You can create a new IntValue with the saved value.

3 Likes

What-

I don’t understand what you mean

If you’re trying to load data saved in a datastore that was originally inside an IntValue or some other Value instance, you have to create the instance again and set the value.

local inst = Instance.new("IntValue")
inst.Name = "QuestTitle"
inst.Value = Data.QuestTitle
ins.Parent = Player.QuestInformation

If you want to automate data handling, make your stored data’s hierarchy the same as that of the instances. Then you can just iterate over all of the player’s children and save the data if they contain value instances.

--didnt test these 2

local f_HierarchyToData; function f_HierarchyToData(o_Parent)
    local d_Data
    for _, o_Child in ipairs(o_Parent:GetChildren()) do
        local v
        if o_Child:IsA("ValueBase") then v=o_Child.Value
        else v = f_HierarchyToData(o_Child)
        end
        if v then
            d_Data = d_Data or {}; d_Data[o_Child.Name] = {_c=o_Child.ClassName, _v = v}
        end
    end
    return d_Data 
end

local f_DataToHierarchy; function f_DataToHierarchy(d_Data, o_Parent)
    for i, d in pairs(d_Data) do
        local s_Class = d._c
        local o_Val = d._v
        local o_Inst = Instance.new(s_Class)
        o_Inst.Name = i
        if  type(o_Val) == "table" then
            f_DataToHierarchy(o_Val, o_Inst)
        else
            o_Inst.Value = o_Val
        end
        o_Inst.Parent = o_Parent
    end
end

--to get data from the player use
local Data = f_HierarchyToData(Player)

--to load data into player use
f_DataToHierarchy(Data, Player)

Also make sure you save around every 30 seconds. Servers sometimes crash and datastore requests can fail.

2 Likes

You could also just create a new instance.new

1 Like

So will it work if I just put this code in mine?

f_HierarchyToData turns all the player’s children into a
data table that can be saved into a datastore. You still need to use UpdateAsync to save the data. (UpdateAsync is more reliable than SetAsync)

f_DataToHierarchy takes a table (created by f_HierarchyToData) and creates new instances within the given parent instance. Obviously you’ll still have to use :GetAsync to obtain the saved data when a player joins.

1 Like