I'm trying to learn some datastore of the roblox wiki but when i wrote the code they had and tried to understand it it worked fine for me. But when why friends joins the server i get an error and i get the message the pcall got me.
Why is this happening, why is it working for the datastore to save the stats for me but not my friends or any other players?
Dictionary cannot be stored in DataStore
DataStore could not be accessed! Data might not save.
Script:
local module = {}
local ToolModule = require(game.ReplicatedStorage.Classes.Modules.Tools)
local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("PlayerData7")
local AUTOSAVE_INTERVAL = 30
local DATASTORE_RETRIES = 3
local PlayerData = {}
function module:ChangeStat(player, statName, changeValue)
PlayerData[player][statName] = changeValue
end
function module:AddValue(player, statName, changeValue)
PlayerData[player][statName] = PlayerData[player][statName] + changeValue
end
function module:ChangeTable(player, statName, item, changeValue)
PlayerData[player][statName][1][item] = changeValue
end
function module:ReturnStats(player)
return PlayerData
end
local function DataStoreRetry(DataStoreFunction)
local tries = 0
local success = true
local data = nil
repeat
tries = tries + 1
success, message = pcall(function() data = DataStoreFunction() end)
print(data)
if not success then wait(1) end
until tries == DATASTORE_RETRIES or success
if not success then
print(message)
error("DataStore could not be accessed! Data might not save.")
end
return success, data
end
function module:GetStats(player)
return DataStoreRetry(function()
return playerData:GetAsync(player.UserId)
end)
end
local function savePlayerData(player)
if PlayerData[player] then
return DataStoreRetry(function()
return playerData:SetAsync(player.UserId, PlayerData[player])
end)
end
end
function LoadData(player)
return DataStoreRetry(function()
return playerData:GetAsync(player.UserId)
end)
end
function SetupPlayerData(player)
local success, data = LoadData(player)
if not success then
PlayerData[player] = false
else
if not data then
print("Save not found, creating a new save!")
PlayerData[player] = {
Cash = 0,
Level = 1,
Experience = 0,
Rebirth = 0,
Rank = "Beginner",
Tool = "GravityGun",
Backpack = "Backpack1",
BackpackAmount = 0,
ToolStats = {
ToolModule,
}
}
savePlayerData(player)
else
PlayerData[player] = data
end
end
end
function AutoSave()
while wait(AUTOSAVE_INTERVAL) do
for player, data in pairs(PlayerData) do
print("Saving Data!")
savePlayerData(player)
end
end
end
spawn(AutoSave)
game.Players.PlayerAdded:Connect(SetupPlayerData)
game.Players.PlayerRemoving:Connect(function(player)
savePlayerData(player)
PlayerData[player] = nil
end)
return module

2 comments
Do you get any errors?
No, no errors only the message that says "Dictonary cannot be stored in Datastore"