Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do you use DataStores and SaveTables to save progress in a tycoon?

Asked by 5 years ago
Edited 5 years ago

Hello, Ive been having this problem for months,I just have no clue how to make a players progress save in a tycoon. I have a DataStore script, and I have tried putting Bool values in that script and setting them to true for each tycoon object, but it is not working. Plz help!

Here is the script im trying to use:

local DataStoreService= game:GetService('DataStoreService'):GetDataStore("PretendCodeIsHere")
local RunService = game:GetService("RunService")
local db = 3

function SaveTableFunc(player)
    local SaveTable = { 
     player={Mine = true}
    }
    return SaveTable
end

game.Players.PlayerAdded:connect(function(player) 
    repeat wait() until player.Character
    repeat wait() until script:FindFirstChild("stats")
  local save1 = script.stats:Clone()
    save1.Parent = player
    local stats = player.stats

    local save = SaveTableFunc(player)
    local playerkey = "player-"..player.userId
    local SaveFiles = DataStoreService:GetAsync(playerkey)
    function dataInput(player, save)

          stats.bank.Value = SaveFiles.bank
          print("Got saved data for "..player.Name)
    end
    local Passed, Error = pcall(function() 
            if SaveFiles == nil then
                print(player.Name.." has no save data yet")
            else
        dataInput(player, save)
            end
    end)
    if Error then
        print("***ERROR GETTING SAVE FILE FOR "..player.Name)
        local attempts = 0
        while wait(db) do
            local retries = attempts + 1
            attempts = retries
            local pass,err = pcall(function()
            if SaveFiles == nil then
                print(player.Name.." has no save data yet")
        else
        dataInput(player, save)
            end
      end)
            if pass or attempts == 5 then break end
        end
    end
end)

function SaveData(player)
    local Success,Error = pcall(function()
        local playerkey = "player-"..player.userId
        local save = SaveTableFunc(player)
        DataStoreService:SetAsync(playerkey,save)
        print("Saved data for "..player.Name)
    end)
    if Error then
        print("***ERROR SAVING DATA FOR "..player.Name)
        local attempts = 0
        while wait (db) do
            local retries = attempts + 1
            attempts = retries
            local pass,fail = pcall(function()
                local playerkey = "player-"..player.userId
                local data = SaveData(player)
                DataStoreService:SetAsync(playerkey,save)
                print("Successfully saved data for "..player.Name)

            end)

            if pass or attempts == 10 then break end

        end

    end
end

game.Players.PlayerRemoving:Connect(SaveData)

game:BindToClose(function() 
    if RunService:IsStudio() then return end
    local Players = game:GetService("Players")
    for _,player in pairs(Players:GetPlayers()) do
        SaveData(player)
        print("Saved data for "..player.Name.." on shutdown.")
    end
end)

Im also using ZedNovs tycoon kit Idk if that makes a difference.

1
please put the whole thing into a code block Warfaresh0t 414 — 5y
0
srry about that mistermaster06 3 — 5y
0
'for each tycoon'? Are you saying you have multiple of these scripts? You don't need multiple DataStore scripts when you can fit it all into one and handle all players. Just have a second tycoon handler that changes the stats for a player. Keep your game logic out of DataSaving. xPolarium 1388 — 5y
0
Sorry, I only have one script, I was trying to say that I had a line of code for each object. Also,what do you mean by a second handler? mistermaster06 3 — 5y
0
The second script will update/apply any stats (like cash or level of a player) for what they do in their tycoon. You should only have the DataStore script used to get saved data, create new data or save it after they leave. The best way is using a table for each player that both scripts cans see/share when needed. xPolarium 1388 — 5y

Answer this question