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

How to save a table using Datastore2?

Asked by
naturedat 124
4 years ago
Edited 4 years ago

Hi, I've been asking this question for 4 times and this is the 5th one so please help me. So basically I added bool values to the folder so I could access the players inventory later in the game and the problem is that it isn't saving.

Datastore2 script:

local DataStore2 = require(1936396537)
local default = {"Wooden Sword","Wooden Bow","Wooden Spear"}
game.Players.PlayerAdded:Connect(function(plr)
    local dataweap = DataStore2("Weapons",plr)

    local fol = Instance.new("Folder")
    fol.Parent = plr
    fol.Name = "Weapons"
    dataweap:Get(default)
    for i,v in pairs(dataweap:Get())do
        local tag = Instance.new("BoolValue")
        tag.Parent = fol
        tag.Name = v
    end

    dataweap:OnUpdate(function(new)
        for i,v in pairs(fol:GetChildren())do -- remving old values in the folder
            v:Destroy()
        end
        for i,v in pairs(dataweap:Get())do -- Adding new bool values
            if game:GetService("ReplicatedStorage").Weapons:FindFirstChild(v)then
                local tag = Instance.new("BoolValue")
                tag.Parent = fol
                tag.Name = v
            end
        end
        dataweap:Set()
    end)
end)

Giver script:

local par = script.Parent
local cool = false
par.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid")and cool == false then
        cool = true
        if game.Players:GetPlayerFromCharacter(hit.Parent)then
            local name = game.Players:GetPlayerFromCharacter(hit.Parent)
            local Datastore2 = require(1936396537)
            local dataweap = Datastore2("Weapons",name)
            local weap = game.ReplicatedStorage.Weapons:GetChildren()
            local ran = weap[math.random(1,#weap)]
            print(ran)
            table.insert(dataweap, ran.Name)
            for i,v in pairs(dataweap:Get())do -- Just to check what is in the data
                print(v)
            end
            print(dataweap)
        end
        wait(7)
        cool = false
    end
end)

The giver gives a player random weapons when they touch it. So this actually print out:

-- Magic Staff

-- Wooden Sword
-- Wooden Bow
-- Wooden Spear

So I think that there is some problem with the table.insert thing in the giver script or is there a different way to put string values into a datastore2 table? Help please, Thank you

0
It's my first time seeing a DataStore being required...What does it do? DiamondComplex 285 — 4y
0
It's some DataStore ModuleScript that simplifies data storage. Rinpix 639 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago

This is a good question, and a lot of people ask this... so hopefully, I can help you out.

Let's start with an example, setting up a DataStore.

local Players = game:GetService("Players")

local DataStore2 = require(1936396537)
DataStore2.Combine("DATA", "weapons")

local default_data = {"Wooden Sword","Wooden Bow","Wooden Spear"}

Players.PlayerAdded:Connect(function(player)
    local weaponsStore = DataStore2("weapons", player)

    -- Get Data & Set Default
    local weapons = weaponsStore:Get(default_data)
end)

Now what this did, is we created the DataStrore, and then we fetched the data and set a default value if no data has been saved under that key... Now, to answer the question about saving tables, rather than using the :Get function, we would use the :Set function. Here is an example:

local new_data = {"Stone Sword", "Wooden Bow", "Wooden Spear"}
weaponsStore:Set(new_data)

I hoped this answer your question, and if it didn't, respond to this post and I will do my best to assist you further! Also, here is a link to the DataStore2 documentation, this should be able to answer all your questions.

Ad

Answer this question