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)
Not exactly sure what the issue here is, everything looks fine.
A few notes:
- plrFolder is in ServerStorage meaning it's not visible in workspace
A few options:
- Add a spawn function to the
for loop
- Move the for loop under the PlayerRemoving event
I don't see any issues in code and the loop is the issue standing out to me.
spawn(function() for i,v in pairs(game.Workspace.Tycoons:GetChildren()) do if v:IsA("Model") then savedTycoonsTable[v.Name] = v:Clone() end end end)
Or move it under the script completely (after the last end)
Hopefully, this helped solve your issue.
Best of luck.