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

how would I save a table to a datastore? [closed]

Asked by 4 years ago

I am trying to store values such as coins in a table and then save the table. I have the values constantly being updated and pcall functions. Whenever I run the script I get the default value of coins and the pcall function error.

--Get The Service's
local replicatedStorage = game:GetService("ReplicatedStorage")
local timePlayedValue = replicatedStorage.PlayerStats:WaitForChild("TimePlayedValue")
local totalTimeValue = replicatedStorage.PlayerStats:WaitForChild("TotalTimeValue")
local httpsService = game:GetService("HttpService")
local dsService = game:GetService("DataStoreService")
local mainStore = dsService:GetDataStore("MainDataStore")

--Get The Gameplay Stats
local CompletedMissionsValue = game:GetService("ReplicatedStorage").GamePlayStats:WaitForChild("CompletedMissionsValue")
local FailedMissionsValue = game:GetService("ReplicatedStorage").GamePlayStats:WaitForChild("FailedMissionsValue")
local PreviousMissionValue = game:GetService("ReplicatedStorage").GamePlayStats:WaitForChild("PreviousMissionValue")
local TimesDiedValue = game:GetService("ReplicatedStorage").GamePlayStats:WaitForChild("TimesDiedValue")
local TrapsAvoidedValue = game:GetService("ReplicatedStorage").GamePlayStats:WaitForChild("TrapsAvoidedValue")
local TrapsFailedValue = game:GetService("ReplicatedStorage").GamePlayStats:WaitForChild("TrapsFailedValue")

--Get The PlayerStats
local CoinsValue = replicatedStorage.PlayerStats:WaitForChild("Coins")
local DateFirstJoinedValue = replicatedStorage.PlayerStats:WaitForChild("DateFirstJoinedValue")
local RankValue = replicatedStorage.PlayerStats:WaitForChild("Rank")
local ExperienceValue = replicatedStorage.PlayerStats:WaitForChild("XP")

--Table To Store Data
local AllStatsTable = {CompletedMissionsValue,FailedMissionsValue,PreviousMissionValue,TimesDiedValue,TrapsAvoidedValue,TrapsFailedValue,CoinsValue,
    DateFirstJoinedValue,RankValue,ExperienceValue,totalTimeValue}


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

coroutine.resume(incrementor)

--FIX DATASTORE
game:GetService("Players").PlayerRemoving:Connect(function(player)
    local success, err = pcall(function()
        mainStore:SetAsync(player.UserId,AllStatsTable)
    end)
    if err then
        print("error saving")
    end
end)

game:GetService("Players").PlayerAdded:Connect(function(player)
local success, err = pcall(function()
        local data = mainStore:GetAsync(player.UserId)
        if data then
            print(data[7])
        else
            print("no data")
        end
    end)
    if err then
        print("error saving")
    end
end)

Closed as Non-Descriptive by hiimgoodpack and WideSteal321

This question has been closed because its title or content does not adequately describe the problem you are trying to solve.
Please ensure that your question pertains to your actual problem, rather than your attempted solution. That is, you were trying to solve problem X, and you thought solution Y would work, but instead of asking about X when you ran into trouble, you asked about Y.

Why was this question closed?

1 answer

Log in to vote
0
Answered by
AizakkuZ 226 Moderation Voter
4 years ago
Edited 4 years ago

Lol, the answer is legit on this website luckily, https://scriptinghelpers.org/guides/saving-and-loading-data-with-datastores thisl ink will tell you basically everything you need to know abt DataStore Service and stuff but, make sure to look at the documentation from the Developer Roblox page also. https://developer.roblox.com/en-us/articles/Saving-Player-Data

0
its blocked and you didn't answer my question jakebball2014 84 — 4y
0
First, get the player with it's key basically like local Key = "KeyHere|"..player.UserId then, you make a table with the values like table = {lol = 20, lol2 = 200} finally, you take the table and data and use DATASTORE NAME:SetAsync(Key, table) to save the data AizakkuZ 226 — 4y
0
Sorry about that I didn't know it was blocked AizakkuZ 226 — 4y
0
thanks soo much! That fixed it! :) jakebball2014 84 — 4y
Ad