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

how do you save tables with data store?

Asked by 4 years ago
Edited 4 years ago

so the script i made is supposed to save a table with items names, and when player joins it should find items from the table in purchasables model (which is a model that has all purchasable items), and put them into purchased model (where bought items are stored) but it surprisingly doesn't work. however im not getting any errors. Heres the script:

local datastore = game:GetService("DataStoreService")
local ds2 = datastore:GetDataStore("DS2")
local purchasables = game.ServerStorage.PURCHASABLES:GetChildren()
game.Players.PlayerAdded:Connect(function(plr)
    local success,err = pcall(function()
        for i,v in pairs(purchasables) do
            if v.Name == val then
                v.Parent = game.Workspace.PURCHASED
            end
        end
    end)
    if success then
        print("Your Items Have Been Loaded.")
    else
        print("Your Items Loading Has Failed.")
        warn(err)
    end
end)

game.Players.PlayerRemoving:Connect(function(plr)
    for i,v in pairs(game.Workspace.PURCHASED:GetChildren()) do
        tab,val = {},v.Name
        table.insert(tab,val)
    end
    local success,err = pcall(function()
        for i,v in pairs(purchasables) do
            ds2:SetAsync(plr.UserId,val)
        end
    end)
    if success then
        print("Your Items Have Been Saved.")
    else
        print("We Got An Error While Saving Your Items.")
        warn(err)
    end
end)

3 answers

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

well the best thing to do is to save the table as JSON, you can make your own JSON parser, or you can use HTTP service function parsers

for instance:

local http= game:GetService("HTTPService")
local data = {name = "badcc", vehicles = {"lambo", "benz", "tesla"} };

local json = http:JSONEncode(data) --turns table to a json  object
local data2 = http:JSONDecode(json) -- turns the json object into a lua object, like "data"

remember, the JSON returned from http:JSONEncode is just a lua string, which means you can save it to datastore

when you call http:JSONEncode(data) it will retun something like this "{name = "badcc", vehicles=["lambo", "benz", "tesla"]}"

you can find out more about the HTTP service here

0
looks even more complicated than data store 2 but ill try it out TFlanigan 86 — 4y
0
w0t...who uses JSONencode in DataStore, like it's so irrelevant to that. The only places i've seen it being used is in Discord Webhooks DrDecor 0 — 4y
0
@DrDecor the point is it turns the data into JSON, yes its defined in HTTP service, but JSON is not just for HTTP. in fact JSON stands for "JavaScript Object Notation", and plus whether you want to or not, you will have to turn your data into a string before saving it to datastore (or sending it over a network, in your case), which the JSON functions are good at User#23252 26 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

this is what i have done so far with the json

local datastore = game:GetService("DataStoreService")
local http = game:GetService("HttpService")
local ds2 = datastore:GetDataStore("DS2")
local purchasables = game.ServerStorage.PURCHASABLES:GetChildren()
game.Players.PlayerAdded:Connect(function(plr)
    local success,err = pcall(function()
        for i,v in pairs(purchasables) do
            data2 = http:JSONDecode(json)
            if v.Name == data2 then
                v.Parent = game.Workspace.PURCHASED
            end
        end
    end)
    if success then
        print("Your Items Have Been Loaded.")
    else
        print("Your Items Loading Has Failed.")
        warn(err)
    end
end)

game.Players.PlayerRemoving:Connect(function(plr)
    for i,v in pairs(game.Workspace.PURCHASED:GetChildren()) do
        data = {v.Name}
        json = http:JSONEncode(data)
    end
    local success,err = pcall(function()
        for i,v in pairs(purchasables) do
            ds2:SetAsync(plr.UserId,json)
        end
    end)
    if success then
        print("Your Items Have Been Saved.")
    else
        print("We Got An Error While Saving Your Items.")
        warn(err)
    end
end)

rip

Log in to vote
0
Answered by 4 years ago
--Try this

local dataStore = game:GetService("DataStoreService")
local DS = dataStore:GetDataStore("DS2")
local purchasables = game.ServerStorage:WaitForChild("PURCHASABLES"):GetChildren()
local purchased = workspace:WaitForChild("PURCHASED")
local purchasedTBL = purchased:GetChildren()

game.Players.PlayerAdded:Connect(function(plr)
    local gameData = DS:GetAsync(plr.UserId)

    for _,v in pairs(purchasables) do
        if gameData == nil then return end --New Player
        gameData[v].Parent = purchased
    end
end)

game.Players.PlayerRemoving:Connect(function(plr)
    local saveData = {}

    for _,v in pairs(purchased) do
        table.insert(saveData,v)
    end
    DS:SetAsync(plr.UserId, saveData)
end)

game:BindToClose(function() --Prevent Data Loss, if unexpected shutdown
    for _,v in pairs(game.Players:GetPlayers()) do
        v:Kick("Closing")
    end

    game:GetService("Players").PlayerAdded:Connect(function(plr)
        plr:Kick("Closing")
    end)
end)
0
argument 1 missing or nil TFlanigan 86 — 4y
0
wait im getting other error like attemp to index nil with parent TFlanigan 86 — 4y
0
Are "PURCHASED" & "PURCHASABLES" both folders? DrDecor 0 — 4y
0
no there models TFlanigan 86 — 4y
0
theyre* TFlanigan 86 — 4y

Answer this question