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

How would I save mutiple values in a table?

Asked by 4 years ago

I am making a datastore that stores a table of mutiple values. I have the player leave with the current values and I get no error. When I enter back in the game I get the default values I set. The only value that saves is the totaltime value which tracks the amount of seconds the player spent in the game ever since thet first joined. I still get no error at all. PS: I enabled API studio access btw.

--Get The Service's
local replicatedStorage = game:GetService("ReplicatedStorage")
local httpsService = game:GetService("HttpService")
local dsService = game:GetService("DataStoreService")
local mainStore = dsService:GetDataStore("MainDataStore")

--create player data
game:GetService("Players").PlayerAdded:Connect(function(plr)

    --create folders

    local leader = Instance.new("Folder",plr)
    leader.Name = "leaderstats"

    local achievementsFolder = Instance.new("Folder",leader)
    achievementsFolder.Name = "Achievements"

    local gameplayStats = Instance.new("Folder",leader)
    gameplayStats.Name = "GameplayStats"

    local playerStats = Instance.new("Folder",leader)
    playerStats.Name = "PlayerStats"


    --create achievements Values

    CompletedTutorial = Instance.new("BoolValue",achievementsFolder)
    CompletedTutorial.Name = "CompletedTutorialAchievement"
    CompletedTutorial.Value = false


    --create gameplay values

    CompletedMissions = Instance.new("IntValue",gameplayStats)
    CompletedMissions.Name = "CompletedMissions"
    CompletedMissions.Value = 0

    FailedMissions = Instance.new("IntValue",gameplayStats)
    FailedMissions.Name = "FailedMissions"
    FailedMissions.Value = 0

    PreviousMission = Instance.new("StringValue",gameplayStats)
    PreviousMission.Name = "PreviousMission"
    PreviousMission.Value = "No Missions Attempted"

    TimesDied = Instance.new("IntValue",gameplayStats)
    TimesDied.Name = "TimesDied"
    TimesDied.Value = 0

    TrapsAvoided = Instance.new("IntValue",gameplayStats)
    TrapsAvoided.Name = "TrapsAvoided"
    TrapsAvoided.Value = 0

    TrapsFailed = Instance.new("IntValue",gameplayStats)
    TrapsFailed.Name = "TrapsFailed"
    TrapsFailed.Value = 0


    --create player values

    Coins = Instance.new("IntValue",playerStats)
    Coins.Name = "Coins"
    Coins.Value = 0


    local currentDay = os.date("*t").day
    local currentMonth = os.date("*t").month
    local currentYear = os.date("*t").year

    local dateJoined = (currentDay.."/"..currentMonth.."/"..currentYear)

    DateFirstJoined = Instance.new("StringValue",playerStats)
    DateFirstJoined.Name = "DateFirstJoined"
    DateFirstJoined.Value = dateJoined

    Rank = Instance.new("StringValue",playerStats)
    Rank.Name = "Rank"
    Rank.Value = "Junior Assistant"

    totalTime = Instance.new("IntValue",playerStats)
    totalTime.Name = "TotalTimePlayed"
    totalTime.Value = 0

    timePlayed = Instance.new("IntValue",playerStats)
    timePlayed.Name = "TimePlayed"
    timePlayed.Value = 0

    Experience = Instance.new("IntValue",playerStats)
    Experience.Name = "Experience"
    Experience.Value = 0

    wait(1)

    --load the data
    local success, err = pcall(function()
        local data = mainStore:GetAsync(plr.UserId) 
        if data then

            --gameplay values
            CompletedMissions.Value = data[1]
            FailedMissions.Value = data[2]
            PreviousMission.Value = data[3]
            TimesDied.Value = data[4]
            TrapsAvoided.Value = data[5]
            TrapsFailed.Value = data[6]

            --player stats value
            Coins.Value = data[7]
            DateFirstJoined.Value = data[8]
            Rank.Value = data[9]
            totalTime.Value = data[10]
            Experience.Value = data[11]

            print(CompletedMissions.Value, FailedMissions.Value, PreviousMission.Value, TimesDied.Value, TrapsAvoided.Value, TrapsFailed.Value, Coins.Value, DateFirstJoined.Value, Rank.Value, totalTime.Value, Experience.Value)

        else
            print("no data")
        end
    end)
    if err then
        print("error loading data")
    end
end)

--Increment Time Values
incrementor = coroutine.create(function()
    while wait(1) do
        timePlayed.Value = timePlayed.Value + 1
        totalTime.Value = totalTime.Value + 1
    end
end)

coroutine.resume(incrementor)

game:GetService("Players").PlayerRemoving:Connect(function(player)

    local DataToSave = {CompletedMissions.Value, FailedMissions.Value, PreviousMission.Value, TimesDied.Value, TrapsAvoided.Value, TrapsFailed.Value, 
    Coins.Value, DateFirstJoined.Value, Rank.Value, totalTime.Value, Experience.Value}

    local success, err = pcall(function()
        mainStore:SetAsync(player.UserId,DataToSave)
    end)
    if err then
        print("error saving")
    end
end)


0
bump jakebball2014 84 — 4y

1 answer

Log in to vote
-2
Answered by
0_2k 496 Moderation Voter
4 years ago

Here's the way I did it

-- >> Functioning
game.Players.PlayerAdded:Connect(function(plr)
    local stats = Instance.new("Folder", plr)
    stats.Name = "leaderstats"

    local afk = Instance.new("BoolValue", stats)
    afk.Name = "AFK"
    afk.Value = false

    local level = Instance.new("IntValue", stats)
    level.Name = "Level"

    local wins = Instance.new("IntValue", plr)
    wins.Name = "Wins"

    local roundsPlayed = Instance.new("IntValue", plr)
    roundsPlayed.Name = "Rounds_Played"

    local team = Instance.new("StringValue", plr)
    team.Name = "Team_Affiliated"

    local key = "user-"..plr.UserId
    local items = playerData:GetAsync(key)

    if items then
        level.Value = items[1]
        wins.Value = items[2]
        roundsPlayed.Value = items[3]
        team.Value = items[4]
        print("Speedy Systems; Successfully retrieved stats")
    else
        local saveItems = {level.Value, wins.Value, roundsPlayed.Value, team.Value}
        playerData:SetAsync(key, saveItems)
        print("Speedy Systems; Stats not received")
    end
end)

-- >> Player Removing
game.Players.PlayerRemoving:Connect(function(plr)
    local stats = plr:WaitForChild("leaderstats")
    local items = {plr.leaderstats.Level.Value, plr.Wins.Value, plr.Rounds_Played.Value, plr.Team_Affiliated.Value}
    local key = "user-"..plr.UserId

    local saved, notSaved = pcall(function()
        playerData:SetAsync(key, items)
    end)

    if saved then
        print("Speedy Systems; Successfully saved data")
    else
        print("Speedy Systems; Error upon saving data")
        warn("Speedy Systems; Error: "..notSaved)
    end
end)
1
Mind explaining. programmerHere 371 — 4y
0
Uh yeah, I used keys and arrays to make it easier to save data, as I'm not spoonfeeding I gave a decent example they could rely on to help themselves out. 0_2k 496 — 4y
Ad

Answer this question