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

DataStore2 How to save a table?

Asked by
Diltz_3 75
6 years ago

Hello! Everyone I has a problem I'm trying to save table but I'm getting

22:29:18.232 - HTTP 0 (HTTP 403 (HTTP/1.1 403 Forbidden)) 22:29:18.232 - DataStore request was added to queue. If request queue fills, further requests will be dropped. Try sending fewer requests. Key = 22:29:19.366 - Data store TestyData_RT5R1 was not saved as it was not updated.

How to fix that?

Script:

local DataStoreModule = require(1936396537)
local Empty = {
    Coins = 0,
    Wins = 0
}

game.Players.PlayerAdded:Connect(function(Player)
    local newFolder = Instance.new("Folder",Player)
    newFolder.Name = "leaderstats"
    local CoinsValue = Instance.new("IntValue",newFolder)
    RepValue.Name = "Coins"
    local WinsValue = Instance.new("IntValue",newFolder)
    WinsValue.Name = "Wins"

    local DataStore = DataStoreModule("TestyData_RT5R1",Player)

    local reqAsync = DataStore:GetTable(Empty)
    if reqAsync then
        CoinsValue.Value = reqAsync.Coins
        WinsValue.Value = reqAsync.Wins
    end
end)
0
Datastore can't save different types of values. Make sure all are strings or integers. Unless they changed this. chexburger 358 — 6y

3 answers

Log in to vote
1
Answered by 6 years ago

Just as angel1001 said.. its quite simple. And you dont necessarily need a module for this. So lets get this correct and explained. :)

So first off id like you to open this link before you go any further: https://www.robloxdev.com/api-reference/class/DataStoreService

Links to rest of functions used here (you dont have to open them unless you dont understand the code below) Storage: https://www.robloxdev.com/api-reference/function/DataStoreService/GetDataStore GetAsync: https://www.robloxdev.com/api-reference/function/GlobalDataStore/GetAsync SetAsync: https://www.robloxdev.com/api-reference/function/GlobalDataStore/SetAsync

Also please remember datastore is SERVER side only!

So here is how easy datastore is to set up (also because you are using a module this will also be a module)

Module named "DataStoreModule" under our parent script

local module = {}

local DS = game:GetService("DataStoreService") -- Grabs the service we need
local Storage = DS:GetDataStore("Test01") -- Our storage

function module:GetPlayerData(Player)
    local Data = Storage:GetAsync(Player.UserId)
    if Data == nil then
        Data = {Wins =0, Coins =0} -- a new user
    end
    return Data
end

function module:SavePlayerData(Player, Data)
    Storage:SetAsync(Player.UserId, Data)
    return true
end

return module

Our script

local DS = require("DataStoreModule")
local Players = game.Players

Players.PlayerAdded:connect(function(p)
    local newFolder = Instance.new("Folder",Player)
        newFolder.Name = "leaderstats"
    local CoinsValue = Instance.new("IntValue",newFolder)
        RepValue.Name = "Coins"
    local WinsValue = Instance.new("IntValue",newFolder)
        WinsValue.Name = "Wins"

    local Data = DS:GetPlayerData(p)
    wait()
    CoinsValue.Value = Data.Coins
    WinsValue.Value = Data.Wins
end)

--[[
And finally to save a players data its simple as this
DS:SavePlayerData(Player, {Coins = CoinsValue.Value, Wins = WinsValue.Value})

(try to save right before they leave the game using Players.PlayerLeaving)
]]--
0
Listen. I'm used DataStore2 because DataStoreService is NOT saving when I'm using game.Players.PlayerRemoving Diltz_3 75 — 6y
1
Its because your module is having a buffer problem, try my code in place of yours. :) MessorAdmin 598 — 6y
0
Also. I know how to work with DataStoreService so please don't tell like "DataStoreService only Server Side!" Diltz_3 75 — 6y
1
Diltz_ Im trying to help as well as angel1001 is, if you want to argue after help is given.. i can just lock your question. MessorAdmin 598 — 6y
View all comments (4 more)
0
Hint: Do not parent stuff to the instance. angeI1001 123 — 6y
0
Making it as easy for him as I can, but myself I make a custom sandbox.. Somewhat like _G environment. MessorAdmin 598 — 6y
0
Way too late likely, but he's using DataStore2, a module that has its own API. Giving him an answer with the other DataStore won't help. Lugical 425 — 5y
0
Way too late likely, but he's using DataStore2, a module that has its own API. Giving him an answer with the other DataStore won't help. Lugical 425 — 5y
Ad
Log in to vote
0
Answered by 3 years ago

Hello! With Datastore2, there is actually a really simple way to save data with tables. Just look at this script:

local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local ServerScriptService = game:GetService("ServerScriptService")

local DataStore2 = require(ServerScriptService:WaitForChild("DataStore2"))

DataStore2.Combine("DATA-Test","MainDataStore-Test")

local defaultTab = {
    AverageTypingSpeed = 0;
    PersonalBestTypingSpeed = 0;
    PersonalBestScore = 0
}

Players.PlayerAdded:Connect(function(player)

    local MainDataStore = DataStore2("MainDataStore-Test",player)

    local Folder = Instance.new("Folder"); Folder.Name = "Values"; Folder.Parent = player

    local ATS = Instance.new("IntValue"); ATS.Name = "AverageTypingSpeed"; ATS.Value = 0;

    local PBTS = Instance.new("IntValue"); PBTS.Name = "PersonalBestTypingSpeed"; PBTS.Value = 0;

    local TypingSpeed = Instance.new("IntValue"); TypingSpeed.Name = "TypingSpeed"; TypingSpeed.Value = 0;

    local Score = Instance.new("IntValue"); Score.Name = "Score"; Score.Value = 0;

    local PBScore = Instance.new("IntValue"); PBScore.Name = "PersonalBestScore";  PBScore.Value = 0;

    local function updateDataStore(updatedValue)
        MainDataStore:SetBackup(5) -- Retry 5 times, give up after
        print("IsBackup:", MainDataStore:IsBackup()) -- Is it using a backup?
        local tab = MainDataStore:Get(updatedValue)
        ATS.Value = tab["AverageTypingSpeed"]
        PBTS.Value = tab["PersonalBestTypingSpeed"]
        PBScore.Value = tab["PersonalBestScore"]
    end

    updateDataStore(defaultTab)

    MainDataStore:OnUpdate(updateDataStore)

    ATS.Parent = Folder
    PBTS.Parent = Folder
    TypingSpeed.Parent = Folder
    Score.Parent = Folder   
    PBScore.Parent = Folder
end)

What's happening here? Well when the function "updateDataStore" is ran, it will first tell DataStore2 that it can try to get the table 5 times before it "pretends" there is no table. (See Here) If they successfully retrieve the table, you can then assign each value that you wanted to be saved to a value in the table.

Example.Value = exampleTab["Example"]

Now, to update/save the table, you can't actually use the increment function for this. You will have to use the Set function instead. I believe this is the only way you can save tables? At least to my knowledge. If you do find another function that could save tables then lmk :)

local newTable = {
    AverageTypingSpeed = 15; -- change to something new 
    PersonalBestTypingSpeed = 5; -- change to something new
    PersonalBestScore = 12 -- change to something new
}

MainDataStore:Set(newTable)

This should work and hopefully you can use this for whatever project your working on :) Have a great day!

Log in to vote
-1
Answered by
angeI1001 123
6 years ago

:SetAsync(userid, table)

Simple as that.

Hope this helped.

0
Um.... That's not DataStore2 Diltz_3 75 — 6y
1
You're using a free model? Then I can't help you. angeI1001 123 — 6y
0
What? I'm just used Module Diltz_3 75 — 6y
0
Datastore2 is not just a free model. It's the best way to save data on roblox. kittonlover101 201 — 5y

Answer this question