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

[SOLVED] Saving tables using DataStore2?

Asked by
8_zn 21
4 years ago
Edited 4 years ago

I scripted a DataStore script using https://devforum.roblox.com/t/how-to-use-datastore2-data-store-caching-and-data-loss-prevention/136317. The script worked at first, but then it suddenly stopped working. Both scripts are located in ServerScriptService, and now I am receiving an error in the output: ServerScriptService.Game.Server.DataStoreManager:38: attempt to index upvalue 'userData' (a number value).

--[[
    author: 8_zn
--]]

--//Services\\--
local Players = game:GetService("Players")
local serverScriptService = game:GetService("ServerScriptService")
local replicatedStorage = game:GetService("ReplicatedStorage")

--//Module\\--
local dataStore2 = require(serverScriptService:WaitForChild("Game"):WaitForChild("Modules"):WaitForChild("DataStoreModule"))

--//Events\\--
local eventsFolder = replicatedStorage:WaitForChild("Events")
local updateCurrencyEvent = eventsFolder:WaitForChild("UpdateCurrencyEvent")

--//Script\\--
local function setupUserData()
    local userData = {
        Currency = {
            ["Fossil"] = 0;
        };

        Dinosaur = {
            ["Tyrannosaurus Rex"] = false;
            ["Allosaurus"] = false;
            ["White Terror"] = false;
        };
    }
    return userData
end

Players.PlayerAdded:Connect(function(Player)
    local userDataStore = dataStore2("UserData", Player)
    local userData = userDataStore:Get(setupUserData())

    local function updateClientCurrency()
        local fossilAmount = userData["Currency"]["Fossil"]
        updateCurrencyEvent:FireClient(Player, fossilAmount)
    end

    updateClientCurrency(userData)
    userDataStore:OnUpdate(updateClientCurrency)
end)

Thank you!

0
I have solved it myself by changing the function name to "updateData" (I don't understand why changing the name worked lol) 8_zn 21 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago

This is what I would do. Not sure about tables, but I figured this out.

--// Setup

local DataStore2 = require(1936396537)

local defaultFossils = 0
local defualtTREX = false
local defualtAllo = false
local defualtWhiRor = false

DataStore2.Combine("MainKey","Fossils","T-REX","Allo","WhiRor")


game.Players.PlayerAdded:Connect(function(Player)

    --// Fossil

    local leaderstats = Instance.new("Folder",Player)
    leaderstats.Name = "leaderstats"

    local fossilsValue = Instance.new("IntValue",leaderstats)
    fossilsValue.Name = "Fossils"

    local fossilStore = DataStore2("Fossils",Player)

    local function updateFossils(value)
        Player.leaderstats.Fossils.Value = value
    end

    updateFossils(fossilStore:Get(defaultFossils))

    fossilStore:OnUpdate(updateFossils)


    --// Dinosaur Data

    local dinosaurData = Instance.new("Folder",Player)
    dinosaurData.Name = "DinosaurData"


    --// T-REX

    local trexStore = DataStore2("T-REX",Player)

    local trexValue = Instance.new("BoolValue",dinosaurData)
    trexValue.Name = "Tyrannosaurus Rex"

    local function updateTREX(value)
        Player.DinosaurData["Tyrannosaurus Rex"].Value = value
    end

    updateTREX(trexStore:Get(defualtTREX))

    trexStore:OnUpdate(updateTREX)


    --// Allo

    local alloStore = DataStore2("Allo",Player)

    local alloValue = Instance.new("BoolValue",dinosaurData)
    alloValue.Name = "Allosaurus"

    local function updateAllo(value)
        Player.DinosaurData.Allosaurus.Value = value
    end

    updateAllo(alloStore:Get(defualtAllo))

    alloStore:OnUpdate(updateAllo)


    --// WhiRor

    local whirorStore = DataStore2("WhiRor",Player)

    local whirorValue = Instance.new("BoolValue",dinosaurData)
    whirorValue.Name = "White Terror"

    local function updateWhiror(value)
        Player.DinosaurData["White Terror"].Value = value
    end

    updateWhiror(whirorStore:Get(defualtWhiRor))

    whirorStore:OnUpdate(updateWhiror)
end)

It's a ServerScript in ServerScriptService.

To test it in studio, put a BoolValue in ServerStorage and name it SaveInStudio and set it to True.

If there are any errors, please let me know so I can fix it for you.

0
This seems inefficient. Like I said, I'm trying to go for tables because its an easier option when you're adding values and etc. 8_zn 21 — 4y
0
I'm not sure, but I know this way works because I tested it using DataStoreName:Set(true) and it worked and saved it. I tried, but I only know complex versions. It's efficient, but not many people understand how DataStore2 works, so I tried my best. Try using youtube or other sources, like wiki's, etc. killerbrenden 1537 — 4y
Ad

Answer this question