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!
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.