Last year I got into roblox development and I decided to start learning with something relatively easy, a Tycoon. As I started to make more complex things, I also wanted to implement a saving method for my game(s), and I stumpled upon a very useful module: DataStore2.
Everything is saved without problem: cash, rebirths and a whole range of different stats. (but they are single values)
The problem seems to have appeared when I tried to save an array/table. I get confirmation that the tycoon is saved, but when the loading process is happening, the array does not contain the same items that it stated it saved.
Here is where a player buys something:
function Purchase(tbl)
local userTycoon = DataStore2("Tycoon", player):Get({}) -the array
local cost = tbl[1]
local item = tbl[2]
local stats = tbl[3]
if not script.Parent.PurchasedObjects:FindFirstChild(item.Object.Value) then
stats.Value = stats.Value - cost
end
Objects[item.Object.Value].Parent = script.Parent.PurchasedObjects -- "item.Object.Value" represents the model name that the button refers to
if not (table.find(userTycoon, item.Object.Value)) then
table.insert(userTycoon,item.Object.Value) -- insert the name of the bought model into the array
DataStore2("Tycoon", player):Set(userTycoon) -- save
print("Added "..item.Object.Value) -- confirm the saving
end
end
And everytime the print works, which leads me to believe that the insertion into the array works.
However, this function is called when a player claims a new tycoon:
local isLoading = false -- variable used so that certain functions won't work when the tycoon is in the saving process
script.Parent.Load.Event:Connect(function(sentPlayer)
if sentPlayer == player then
isLoading = true
print("Loading the tycoon")
local userTycoon = DataStore2("Tycoon",sentPlayer):Get({})
local Buttons = script.Parent.Buttons
if userTycoon ~= nil then
for i,v in pairs(userTycoon) do
print("Contain: "..userTycoon[i]) -showing the array with the saved items names
end
for i, v in pairs(userTycoon) do
for _, x in pairs(Buttons:GetChildren()) do
if x.Object.Value == v and not script.Parent.PurchasedObjects:FindFirstChild(v) then
Purchase({[1] = 0, [2]=x, [3] = game.ServerStorage.PlayerMoney:FindFirstChild(sentPlayer.Name)})
print("Loaded "..x.Object.Value)
x.Head.Transparency = 1 -- clear buttons
x.Head.CanCollide = false
end
end
end
end
isLoading = false
else
return
end
end)
But here it seems that, when I check what values does the saved array contains, some data is not shown.
An example where an item is confirmed to be saved (BioWall4), but is not present in the saved array:
In some post regarding something the same about table saving, it was said to use “GetTable()”, but it was stated that there is no difference and you can safely use “Get({})”.
Any idea what the problem might be?