Hello, I tried sending the dataStore
information to my client for local updates (read only visuals), but apparently my dictionary is cyclic. This error only triggers when I try to send the table to the client, but after reading other topics I’m assuming that this is a flaw in DataStore2 as a whole.
Here’s my DataSetup script
local DataStore2 = require(1936396537)
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")
local function setup(player)
-- Setup the player data
local Data = Instance.new("Folder", player)
Data.Name = "Data"
local Currencies = Instance.new("Folder", Data)
Currencies.Name = "Currencies"
local Backpack = Instance.new("Folder", Data)
Backpack.Name = "Backpack"
-- EDIT: Change this dictionary
local dataStores = {
Coins = { -- Key: ["Coins"], dataInfo: {}
instanceType = "IntValue",
parent = Currencies,
default = 10
},
Souls = {
instanceType = "IntValue",
parent = Currencies,
default = 0
},
EquippedWeapon = {
instanceType = "StringValue",
parent = Backpack,
default = "Fork"
},
BoughtWeapons = {
instanceType = "StringValue",
parent = Backpack,
default = {
"Fork"
}
},
CurrentItems = {
instanceType = "StringValue",
parent = Backpack,
default = {}
},
UpgradeLevels = {
instanceType = "StringValue",
parent = Data,
default = {
["More Damage"] = 0,
["Faster Weapons"] = 0
}
}
}
-- if I want to convert these to methods I'll need to share a metatable with all datastores
local function update(dataInfo)
dataInfo.instance.Value = dataInfo.dataStore:Get(dataInfo.default)
end
local function updateTable(dataInfo)
local dataTable = dataInfo.dataStore:GetTable(dataInfo.default)
for key, val in pairs(dataTable) do
local keyy, number = tostring(key):gsub(" ","") -- removes spaces
if number > 0 then
dataInfo.instance:SetAttribute(keyy, val)
else
dataInfo.instance:SetAttribute(key, val)
end
end
end
_G.set = function(val, dataInfo)
dataInfo.dataStore:Set(val, dataInfo.default)
update(dataInfo)
end
_G.setTable = function(val, dataInfo)
dataInfo.dataStore:Set(val, dataInfo.default)
updateTable(dataInfo)
end
_G.increment = function(val, dataInfo)
dataInfo.dataStore:Increment(val, dataInfo.default)
update(dataInfo)
end
for key, dataInfo in pairs(dataStores) do
dataInfo.dataStore = DataStore2(key, player) -- adds the "cyclic" table
dataInfo.instance = Instance.new(dataInfo.instanceType, dataInfo.parent)
dataInfo.instance.Name = key
if typeof(dataInfo.default) ~= "table" then
update(dataInfo)
dataInfo.dataStore:OnUpdate(update(dataInfo))
else
updateTable(dataInfo)
dataInfo.dataStore:OnUpdate(updateTable(dataInfo))
end
end
-- The player data is ready and public
_G[player.Name.."dataStores"] = dataStores
ServerStorage.Events.DataSetupFinished:Fire(player)
ReplicatedStorage.Events.DataSetupFinished:FireClient(player)
end
-- looking to shorten this line
ServerStorage.Events.BeginDataStoreConfig.Event:Connect(function(player)setup(player)end)
And when I print the tables here’s what I see inside the .dataStore
element
At the end of the day I want to know whether this cyclic table behavior can be fixed on my end and also I’d like to know why when I use :GetAttributes()
on ["instance"]
it always returns 0 regardless of me seeing multiple attributes locally and server side.