-
What do you want to achieve? I am making a simple data saving script that loads data from a datastore onto instances (like intvalues, boolvalues etc.) in folders in the player. At the end of this session, a function reads and retrieves the data from these values and puts them into the table to save.
-
What is the issue? The save data isn’t updating correctly, even though the table is being printed correctly immediately before saving. There are no errors in the console.
-
What solutions have you tried so far? I have tried using updateasync() instead and specifying where prints come from to make it more clear.
Entire script:
local players = game.Players
local datastoreserv = game:GetService("DataStoreService")
local coins_store = datastoreserv:GetDataStore("Coins")
local tools_store = datastoreserv:GetDataStore("Tools")
local tempstats_store = datastoreserv:GetDataStore("TemporaryStats")
--Player-independent functions
function tobool(str)
if str == "true" or str == "false" or str == "nil" then
local boolNew
if str == "true" then
boolNew = true
elseif str == "false" then
boolNew = false
elseif str == "nil" then
boolNew = nil
end
return boolNew
else
warn("Parameter must be a string!")
return
end
end
function test()
print("a")
end
function printTable(chosen_table)
print("BREAK FOR READABILITY")
print("BREAK FOR READABILITY")
for i, v in chosen_table do
print("i = "..tostring(i)..", v = "..tostring(v))
end
end
local table_to_save = {}
function getValuesFromInstances(target_folder)
print("a")
if table_to_save ~= nil then
table.clear(table_to_save)
else
local table_to_save = {}
end
print("a2")
for i, v in target_folder:GetChildren() do
table.insert(table_to_save, v.Name)
table.insert(table_to_save, v.Value)
end
printTable(table_to_save)
print("that was from getValuesFromInstances!")
return table_to_save
end
function instanceload(values_table, target_folder, type_of_instance)
for i, v in values_table do
if i % 2 ~= 0 then
local newInstance = Instance.new(type_of_instance)
newInstance.Name = v
newInstance.Value = values_table[i+1]
newInstance.Parent = target_folder
elseif i % 2 == 0 then
else
print("error!")
end
end
end
players.PlayerAdded:Connect(function(player)
userId = player.UserId
--Setting leaderstats
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
coins = Instance.new("IntValue")
coins.Name = "Coins"
coins.Parent = leaderstats
coins.Value = 0
--Setting hidden values (tables + folders)
--NOTE: tools_owned and similar tables are only used in pre-data loading (instance generation)
wait(3)
tools_table = {"rustybucket", false, "woodenbucket", false}
tempstats_table = {"capacity", 0, "max_capacity", 10}
printTable(tools_table)
printTable(tempstats_table)
local savedcoins = coins_store:GetAsync(userId)
print("You have "..tostring(savedcoins).." coins!")
coins.Value = savedcoins
--Setting hidden values (folders)
tools_folder = Instance.new("Folder")
tools_folder.Name = "ToolsFolder"
tools_folder.Parent = player
tempstats_folder = Instance.new("Folder")
tempstats_folder.Name = "TempStats"
tempstats_folder.Parent = player
instanceload(tools_table, tools_folder, "BoolValue")
instanceload(tempstats_table, tempstats_folder, "NumberValue")
end)
players.PlayerRemoving:Connect(function()
local setSuccess, errorMessage = pcall(function()
coins_store:SetAsync(userId, coins.Value)
--Table stuff
tools_store:SetAsync(userId, tools_table)
tempstats_store:SetAsync(userId, tempstats_table)
print("in progress!")
local table_to_save = getValuesFromInstances(tools_folder)
tools_store:SetAsync(userId, table_to_save)
table.clear(table_to_save)
table_to_save = getValuesFromInstances(tempstats_folder)
tempstats_store:SetAsync(userId, table_to_save)
table.clear(table_to_save)
print("done!")
end)
if not setSuccess then
errorMessage = "Data not saved!"
warn(errorMessage)
end
end)
What console is printing:
(table_to_save here DOES print correctly, but it doesn’t LOAD this:
Thanks for reading this post!