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:

01local DataStore2 = require(1936396537)
02local default = {"Wooden Sword","Wooden Bow","Wooden Spear"}
03game.Players.PlayerAdded:Connect(function(plr)
04    local dataweap = DataStore2("Weapons",plr)
05 
06    local fol = Instance.new("Folder")
07    fol.Parent = plr
08    fol.Name = "Weapons"
09    dataweap:Get(default)
10    for i,v in pairs(dataweap:Get())do
11        local tag = Instance.new("BoolValue")
12        tag.Parent = fol
13        tag.Name = v
14    end
15 
View all 29 lines...

Giver script:

01local par = script.Parent
02local cool = false
03par.Touched:Connect(function(hit)
04    if hit.Parent:FindFirstChild("Humanoid")and cool == false then
05        cool = true
06        if game.Players:GetPlayerFromCharacter(hit.Parent)then
07            local name = game.Players:GetPlayerFromCharacter(hit.Parent)
08            local Datastore2 = require(1936396537)
09            local dataweap = Datastore2("Weapons",name)
10            local weap = game.ReplicatedStorage.Weapons:GetChildren()
11            local ran = weap[math.random(1,#weap)]
12            print(ran)
13            table.insert(dataweap, ran.Name)
14            for i,v in pairs(dataweap:Get())do -- Just to check what is in the data
15                print(v)
View all 22 lines...

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

1-- Magic Staff
2 
3-- Wooden Sword
4-- Wooden Bow
5-- 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.

01local Players = game:GetService("Players")
02 
03local DataStore2 = require(1936396537)
04DataStore2.Combine("DATA", "weapons")
05 
06local default_data = {"Wooden Sword","Wooden Bow","Wooden Spear"}
07 
08Players.PlayerAdded:Connect(function(player)
09    local weaponsStore = DataStore2("weapons", player)
10 
11    -- Get Data & Set Default
12    local weapons = weaponsStore:Get(default_data)
13end)

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:

1local new_data = {"Stone Sword", "Wooden Bow", "Wooden Spear"}
2weaponsStore: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