So when writing my playerdata module which works with tables and datastore 2 it simply wouldn’t save. The datastore2 module is up-to-date, saveinstudio is enabled and this also doesn’t work on the actual roblox client. Weirdly, getting the table does work as one time it randomly did save, and i do not know why. Everytime after that the table was changed. When i print the changed table in the setdata function it is changed. The actual :Set() function of the datastore2 seems to be the problem.
The error “Not running script because past shutdown deadline” seems to be also coming from datastore2 and hangs my roblox studio everytime i try to stop the game. Besides that datastore prints “PLAYER left, saved DATA”
Weirdly the simulator example on the datastore2 api does work in the same game, working with the same module. Here’s the code:
local httpsService = game:GetService("HttpService")
local DataStore2 = require(game:GetService("ServerScriptService"):WaitForChild("Modules"):WaitForChild("DataStore2"))
DataStore2.Combine("DATA", "playerData")
local playerModule = {}
local defaultTable = {
["xp"] = 0,
["coins"] = 0,
["crystals"] = 0,
["inventory"] = {}, --item = {amount}
["storage"] = {},
["placedOnPlot"] = {}, --{item, pos, upgrades}
}
--[[
local DataStoreService = game:GetService("DataStoreService")
local nicknameStore = DataStoreService:GetDataStore("plrData")
nicknameStore:RemoveAsync(388243015)
--]]
--defaultTable = httpsService:JSONEncode(defaultTable)
--Credit to Forummer, Thanks!
local function SetTableFromIndices(t, is, v) --table, table of indices, value.
for i = 1, #is - 1 do
if not t[is[i]] then t[is[i]] = {} end
t = t[is[i]]
end
t[is[#is]] = v
end
function playerModule.GetDataTable(plr)
local plrDataStore = DataStore2("playerData", plr)
local plrDataTable = plrDataStore:GetTable(defaultTable)
print(plrDataTable)
return plrDataTable
end
function playerModule.GetData(plr, indices)
local data = playerModule.GetDataTable(plr)
local result = data
for i, v in pairs(indices) do
result = result[v]
if result == nil then
warn("No value exist for this index table:"..httpsService:JSONEncode(indices))
result = nil
break
end
end
return result
end
function playerModule.SetData(plr, indices, setTo)
local plrDataStore = DataStore2("playerData", plr)
local data = playerModule.GetDataTable(plr)
SetTableFromIndices(data, indices, setTo)
print(data)
plrDataStore:Set(data)
end
function playerModule.Increment(plr, indices, increment)
local dataValue = playerModule.GetData(plr,indices) or 0
if type(dataValue) ~= "number" and type(dataValue) == nil then
error("The value for these indices is not a number:"..httpsService:JSONEncode(indices).."(Type:"..typeof(dataValue)..")")
end
dataValue += increment
playerModule.SetData(plr, indices, dataValue)
end
--[[
game.Players.PlayerRemoving:Connect(function(plr)
local plrDataStore = DataStore2("playerData", plr)
plrDataStore.Save()
end)
game:BindToClose(function()
for i, v in pairs(game.Players:GetChildren()) do
local plrDataStore = DataStore2("playerData", v)
plrDataStore.Save()
end
end)
--]]
return playerModule