I’m so confused on why it’s not saving the folder, if you know why it’s not let me know
local dataModule = {}
local dataStoreService = game:GetService("DataStoreService")
local playerData = dataStoreService:GetDataStore("Testing")
local players = game:GetService("Players")
local sessionData = {}
local autoSave = 30
local runService = game:GetService("RunService")
--local isSudio = runService:IsStudio()
local function getDefaultData()
return {
Coins = 0,
SkinsInventory = {}
}
end
local function createPlayerStats(player)
local playerFolder = Instance.new("Folder")
playerFolder.Name = "Player Folder"
playerFolder.Parent = player
local playerItems = Instance.new("Folder")
playerItems.Name = "Player Items"
playerItems.Parent = playerFolder
local skinsInventory = Instance.new("Folder")
skinsInventory.Name = "Skins Inventory"
skinsInventory.Parent = playerItems
local coins = Instance.new("IntValue")
coins.Name = "Coins"
coins.Parent = playerFolder
end
local function updateStats(player)
local key = "Player_"..player.UserId
local coins = player["Player Folder"].Coins
local skinsInventory = player["Player Folder"]["Player Items"]["Skins Inventory"]
if sessionData[key]["Coins"] then
coins.Value = sessionData[key]["Coins"]
end
if sessionData[key]["SkinsInventory"] then
for i, v in pairs(skinsInventory:GetChildren()) do
table.insert(sessionData[key]["SkinsInventory"], v)
end
end
end
function dataModule:ChangeStat(player, statName, value)
local key = "Player_"..player.UserId
assert(typeof(sessionData[key][statName]) == typeof(value), "Changed error: types do not match!")
sessionData[key][statName] = value
updateStats(player)
end
function dataModule:GetStat(player, statName)
local key = "Player_"..player.UserId
return sessionData[key][statName]
end
function dataModule:IncrementStat(player, statName, value)
local key = "Player_"..player.UserId
assert(typeof(value) == "number", "IncrementStat error: types do not match!")
assert(typeof(sessionData[key][statName]) == "number", "IncrementStat error: type must be a number!")
sessionData[key][statName] += value
updateStats(player)
end
local function setupData(player)
createPlayerStats(player)
local key = "Player_"..player.UserId
local success, data = pcall(function()
return playerData:GetAsync(key)
end)
if success then
if data then
sessionData[key] = data
else
sessionData[key] = getDefaultData()
end
updateStats(player)
else
warn("An error has occurred with setting up the player data!")
end
end
local function saveData(key)
if sessionData[key] then
local tries = 0
local success, err
repeat
tries = tries + 1
success, err = pcall(function()
playerData:UpdateAsync(key, function(oldValue)
return sessionData[key]
end)
end)
if not success then
wait(3)
end
until tries == 3 or success
if not success then
print("An error has occurred with saving the player data!")
warn(err)
--else
--print("Data saved!")
end
end
end
local function saveOnExit(player)
--if isSudio == true then
--return true
--else
local key = "Player_"..player.UserId
saveData(key)
--end
end
local function autoSaveData()
--if isSudio == true then
--return true
--else
while wait(autoSave) do
for i, v in pairs(sessionData) do
saveData(i)
--end
end
end
end
spawn(autoSaveData)
players.PlayerAdded:Connect(setupData)
players.PlayerRemoving:Connect(saveOnExit)
game:BindToClose(function()
--if isSudio == true then
--return true
--else
for i, v in pairs(sessionData) do
saveData(i)
--end
end
end)
return dataModule