I'm making a tycoon game and currently in the development of a tycoon saving / loading system. However, I've run into a problem. Either the saving or loading part isn't working, but I don't know which one. I posted a question focusing only on the saving part, and nobody found anything wrong with it. I'm posting both the saving and loading in this question, just in case. Please, if you find anything wrong with the script that needs to be fixed, tell me. I've been working on debugging this script for over 2 days now and I'd like to be done with it.
Variables
local service = game:GetService('DataStoreService')
local store = service:GetDataStore('OrefareTycoon2Data')
local db = true
Save Function
function saveTycoon(plr)
local tycoon = plr:WaitForChild('PlayerValues').OwnsTycoon.Value
local purchased = tycoon:WaitForChild('PurchasedObjects')
local tbl1 = {}
for _, v in pairs(purchased:GetChildren()) do
table.insert(tbl1, v.Name)
end
local succ, err = pcall(function()
store:SetAsync(plr.UserId..'-cash', plr.leaderstats.Cash.Value)
end)
local succ2, err2 = pcall(function()
store:SetAsync(plr.UserId..'-tycoon', tbl1)
end)
if succ then
print('Saved Cash Data.')
else
print('Failed To Save Cash Data.')
warn(err)
end
if succ2 then
print('Saved Tycoon Data.')
else
print('Failed To Save Tycoon Data.')
warn(err2)
end
end
Load Function
function loadTycoon(plr)
local tycoon = plr:WaitForChild('PlayerValues').OwnsTycoon.Value
local tcnData
local succ, err = pcall(function()
tcnData = store:GetAsync(plr.UserId..'-tycoon')
end)
if succ then
print('Retrieved Tycoon Data.')
else
warn(err)
end
local clone = game.ServerStorage:WaitForChild('TycoonClones'):WaitForChild(tycoon.Name..'_clone')
local newClone = clone:Clone()
print('Loading Data...')
newClone.Parent = tycoon.Parent
newClone.Name = tycoon.Name
tycoon:Destroy()
local info = newClone.TycoonInfo
local gate = newClone.Essentials.Entrance.Gate
plr.PlayerValues.OwnsTycoon.Value = newClone
info.Owner.Value = plr
gate.BillboardGui.Frame.TextLabel.Text = plr.Name.."'s Tycoon"
gate.Transparency = 0.5
plr.Team = game.Teams:WaitForChild(newClone.Name)
local purchases = newClone.Purchases:GetChildren()
local purchased = newClone.PurchasedObjects:GetChildren()
local buttons = newClone.Buttons:GetChildren()
for i = 1, #purchases do
local v = purchases[i]
if table.find(tcnData, v) then
print('Object found in table.')
v.Parent = newClone.PurchasedObjects
for e = 1, #buttons do
local q = buttons[e]
print('for loop 2 entered')
if q:FindFirstChild('Object') and q:FindFirstChild('Object').Value == v.Name then
q:Destroy()
elseif q:FindFirstChild('Dependency') and q:FindFirstChild('Dependency').Value == v.Name then
q.Union.Transparency = 0
q.Union.CanCollide = true
q.Union.BillboardGui.Enabled = true
end
end
else
print('Could not find object in table.')
end
end
end
Other
game.Players.PlayerAdded:Connect(function(plr)
local cash
local success, errorMsg = pcall(function()
cash = store:GetAsync(plr.UserId..'-cash')
end)
if success and cash ~= nil then
print('Retrieved Cash Data.')
plr:WaitForChild('leaderstats').Cash.Value = cash
plr:WaitForChild('PlayerValues').OwnsTycoon.Changed:Connect(function()
if plr:WaitForChild('PlayerValues').OwnsTycoon.Value ~= nil and db == true then
db = false
loadTycoon(plr)
wait(.1)
db = true
end
end)
else
print('An error occurred whilst fetching cash data. (Most likely there was none)')
warn(errorMsg)
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
if plr.PlayerValues.OwnsTycoon.Value ~= nil then
saveTycoon(plr)
end
end)
game:BindToClose(function()
local plrs = game.Players:GetPlayers()
for i = 1, #plrs do
local plr = plrs[i]
if plr.PlayerValues.OwnsTycoon.Value ~= nil then
saveTycoon(plr)
end
end
end)
Sorry for such a long question, but I'm tired of debugging and I've tried everything I can think of. Thanks for taking time to view this question! Edit: Fixed some spelling

1 comment
Also, no errors in the output.