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

DataSave question with saving/loading tables?

Asked by
dirk2999 103
6 years ago

Heres my script,

local carts = {
    ["red"] = 1,
    ["white"] = 2,
    ["blue"] = 3
}

local DSService = game:GetService('DataStoreService')
local Save = DSService:GetDataStore('Carts1')

game.Players.PlayerAdded:connect(function(player)
    pcall(function()
        player:WaitForChild("PlayerGui")
        local saveData = Save:GetAsync(player.userId)
        Save:SetAsync(player.userId,carts)
        print("Done!")
        wait(4)
        if saveData then
            print("Done")
            print(saveData)
        end 
    end)
end)


Output-

23:31:09.115 - RC2 Testing was auto-saved Done! Done table: 2EF00F94

How do I get the table out of the data save using :SetAsync? All I'm getting is that output.

1 answer

Log in to vote
0
Answered by
mattscy 3725 Moderation Voter Community Moderator
6 years ago
Edited 6 years ago

To create an IntValue from all values in the table, you can go:

for i,v in pairs(saveData) do
    local val = Instance.new("IntValue")
    val.Value = v
    val.Name = i
    val.Parent = ...
end

Or to get the value for a specific colour, you can go:

print(saveData["white"]) --> 2
0
Basically what I want the script to eventually do is get all the information in the save data, and create an IntValue for each value, the name being the string, the value being the number. How would I go about that? dirk2999 103 — 6y
0
Edited the top script for this mattscy 3725 — 6y
0
One more question, how would I do it the opposite way around? To save the players 'IntValues' how would I insert their int names and values into a table? dirk2999 103 — 6y
Ad

Answer this question