-- takes string, player object, table of strings
DataStoreWrapper.dataBase = function (storeType, player, statsArray)
-------------------------- Local Functions
-- takes an array of strings, player object
local _stringArrayToDictionary = function (stats, player)
--[[ Parses an array of strings and returns a dictionary of corresponding player leaderstat k,v pairs ]]
local playerLeaderStatsFolder = player:FindFirstChild('leaderstats')
local leaderstats = {
}
for index=1, #stats do
leaderstats[stats[index]] = playerLeaderStatsFolder[stats[index]].Value -- 'Stat' = stat.Value
end
--DEBUG
print('_stringArrayToDictionary()')
for k,v in pairs(leaderstats) do
print('\tKey: ' .. k .. ' | Value: ' .. tostring(v) )
end
print('_stringArrayToDictionary() ending')
return leaderstats
end
-------------------------- dataBase function
local playerUserId = "Player_" .. player.UserId
-- check for existing data, and then load
if storeType == 'load' then
print('Loading Data...')
local data -- declare for memory efficiency
local success, errormessage = pcall(function()
data = DataStoreWrapper.myDataStore:GetAsync(playerUserId)
end)
-- Error Handler
if success then
-- for every leaderstat.Value and loaded data value, set them equal to each other to load in
for key,leaderstatVal in pairs(_stringArrayToDictionary(statsArray, player)) do
for key2, dataVal in pairs(data) do
key = dataVal -- set data equal to leaderstat value
--DEBUG
print("\tleads Key: " .. key .. ' | leads value: '.. leaderstatVal)
print("\tdatas Key: " .. key2 .. ' | datas value: '.. dataVal)
end
end
print('Data loaded successfully!')
elseif errormessage then
print('[SEVERE] WARNING... DATASTORE LOAD ERROR: ' .. warn(errormessage))
end
elseif storeType == 'save' then
print('Saving Data...')
local saveData = _stringArrayToDictionary(statsArray, player)
local success, errormessage = pcall(function()
DataStoreWrapper.myDataStore:SetAsync(playerUserId, saveData) -- save
end)
-- Error Handler
if success then
print("Data saved successfully!")
--DEBUG (not displayed strangely)
for k,v in pairs(saveData) do
print("\t" .. k .. ': '.. v)
end
elseif errormessage then
print('[SEVERE] WARNING... DATASTORE SAVE ERROR: ' .. warn(errormessage) )
end
end
end
The above code is in a ModuleScript. If I were to require this in a server script, and would like to save/load the player’s current Coins
leaderstat value, I would do so with
dataStoreWrapper.dataBase('save', player, {'Coins'})
dataStoreWrapper.dataBase('load', player, {'Coins'})
Multiple leaderstat values can be saved by adding to the array.
The problem is that the leaderstats aren’t set to the saved data when loaded. Further debugging by printing values,
for key,leaderstatVal in pairs(_stringArrayToDictionary(statsArray, player)) do
for key2, dataVal in pairs(data) do
key = dataVal -- set data equal to leaderstat value
print("\tleads Key: " .. key .. ' | leads value: '.. leaderstatVal)
print("\tdatas Key: " .. key2 .. ' | datas value: '.. dataVal)
end
end
shows that even though the leaderstat key value pair should be 'Coins' = x
, the leaderstat key is displayed as being an integer. I understand I could simply use the already saved data key value pairs, however it boggles me as to why the first set of key value pairs aren’t the expected values.
Data is saved when the player leaves, or the server shuts down, and is loaded when the player joins.