Here is the code:
local savedTycoonsTable = {}
for i,v in pairs(game.Workspace.Tycoons:GetChildren()) do
if v:IsA("Model") then
savedTycoonsTable[v.Name] = v:Clone()
end
end
game.Players.PlayerRemoving:Connect(function(plr)
if plr.CanSave.Value then
local specialKey = "User_"..plr.UserId
local plrName = plr.Name
local plrInfo = game.ServerStorage:FindFirstChild(plrName):WaitForChild("PlayerInfo")
if plrInfo.OwnsTycoon.Value then
local tycoonName = plrInfo.OwnedTycoon.Value
local tycoon = game.Workspace.Tycoons:FindFirstChild(tycoonName)
local moneyToSave = plrInfo.Money.Value
local buildLevelToSave = plrInfo.BuildLevel.Value
local purchasedItemsTable = getTableForDataStore(tycoon, plrInfo)
local mainTableToSave = {moneyToSave, buildLevelToSave, purchasedItemsTable}
local success, message = pcall(function()
mainDataStore:SetAsync(specialKey, mainTableToSave)
end)
if success then
print(plrName.."'s data saved successfully")
else
print(plrName.."'s data failed to save successfully")
wait(15)
local maxAttemptsAllowed = 3
local saveAttempts = 0
repeat
saveAttempts = saveAttempts + 1
local success, message = pcall(function()
mainDataStore:SetAsync(specialKey, mainTableToSave)
end)
if success then
print(plrName.."'s data saved successfully on try "..saveAttempts)
else
print(plrName.."'s data failed to save successfully on try "..saveAttempts)
end
until success or saveAttempts == maxAttemptsAllowed
end
end
end
wait(30)
local plrName = plr.Name
local plrFolder = game.ServerStorage:FindFirstChild(plrName)
local tycoonName = plrFolder.PlayerInfo.OwnedTycoon.Value
local tycoon = game.Workspace.Tycoons:FindFirstChild(tycoonName)
local tycoonCopy = savedTycoonsTable[tycoon.Name]:Clone()
wait()
plrFolder:Destroy() -- it should be destroying this stuff I have no idea why its not
tycoon:Destroy()
wait()
tycoonCopy.Parent = game.Workspace.Tycoons
end)

2 comments
You helped in more ways than you can know. Thank you so much!
Np