Hello everyone!
I’m (trying to) make a game, and I’ve come to the point where I need to store the player’s stats, inventory etc… I’ve decided on using DataStore2, and specifically dictionaries for storing data. My main problem is that I can’t get the table (dictionary) from the DataStore in another script, and update the dictionary from it.
This is my DataStore2 script for leaderstats. The stats that I’ve added to the leaderstats show fine in-game.
local Players = game:GetService("Players")
local ServerScriptService = game:GetService("ServerScriptService")
local Workspace = game:GetService("Workspace")
local MainKey = "Key" -- To change?
local DataStore2 = require(ServerScriptService.DataStore2)
-- Keys for Values. To add more eventually
DataStore2.Combine(MainKey, "Stats","Inventory","Beaver","Tails","Backpacks","Axes","Quests","Badges","Purchases")
local function SetDataTable()
local PlayerDataTable = {
Stats = {
["Wood"] = 0;
["Total Wood"] = 0;
["Water Damed"] = 0;
["Total Water Damed"] = 0;
};
Inventory = {
["Maple Syrup"] = 5;
};
Beaver = {
["Beaver Level"] = 1;
["EXP"] = 0;
["Beaver Wood"] = 0;
["Beaver Total Wood"] = 0;
};
-- to serialize these?
Tails = {
["Nature"] = true;
-- etc.....
return PlayerDataTable
end
Players.PlayerAdded:Connect(function(player) -- set the stats as variable
local PlayerData = DataStore2(MainKey, player):Get(SetDataTable())
local statsStore = DataStore2("Stats", player)
local invStore = DataStore2("Inventory", player)
local beavStore = DataStore2("Beaver", player)
local tailsStore = DataStore2("Tails", player)
local backStore = DataStore2("Backpacks", player)
local axesStore = DataStore2("Axes", player)
local questsStore = DataStore2("Quests", player)
local badgesStore = DataStore2("Badges", player)
local purchStore = DataStore2("Purchases", player)
statsStore:SetBackup(5)
invStore:SetBackup(5)
beavStore:SetBackup(5)
tailsStore:SetBackup(5)
backStore:SetBackup(5)
axesStore:SetBackup(5)
questsStore:SetBackup(5)
badgesStore:SetBackup(5)
purchStore:SetBackup(5)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
local Wood = Instance.new("NumberValue")
Wood.Name = "Wood Chopped"
local Water = Instance.new("NumberValue")
Water.Name = "Water Bottled"
local function updateLeader(updatedTable)
Wood.Value = DataStore2(MainKey,player):Get(updatedTable)["Stats"]["Wood"]
Water.Value = DataStore2(MainKey,player):Get(updatedTable)["Stats"]["Water"]
end
updateLeader(PlayerData)
DataStore2(MainKey, player):OnUpdate(updateLeader)
leaderstats.Parent = player
Wood.Parent = leaderstats
Water.Parent = leaderstats
end)
This is my other script in which I want to update the Wood value of Stats. It worked fine when I used DataStore2 normally, without tables.
-- main script for changing wood value. to be changed to a module
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteFunction = ReplicatedStorage.RemoteEvents:WaitForChild("toolEquip")
local Players = game:GetService("Players")
local toolstats = require(game.ServerStorage.Modules.toolStats)
local trees = require(game.ServerStorage.Modules.treeList)
local DataStore2 = require(game.ServerScriptService.DataStore2)
local MainKey = "Key"
debounce = false
local function woodChop(player,tool)
--etc...
local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)
if raycastResult then
if debounce == false then
debounce = true
local PlayerData = DataStore2(MainKey, player):Get(SetDataTable()) -- !!!!!! MainKey, or "Stats"?
print(PlayerData)
--etc....
end
end
end
tool.Activated:Connect(onChop)
end
remoteFunction.OnServerInvoke = woodChop
When I try to get the dictionary (table) that has Stats, Inventory… it always returns “Attempt to call a nill value”.
local PlayerData = DataStore2(MainKey, player):Get(SetDataTable()) --MainKey, or Stats?
I’ve tried Get, GetTable, basically every answer from the DevForum and the internet, and it always returns that I’m calling a nill value.
I’ll be forever grateful if anyone can help!