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

DataSave, why is this save returning nil?

Asked by
dirk2999 103
6 years ago

This is a test script to see if I can connect a string with a certain number while saving them. Red-1,White-2,Blue-3. saveData is nil, I don't understand why?

carts = {"Red", "White", "Blue"}
test = {1,2,3}

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

game.Players.PlayerAdded:connect(function(player)
    pcall(function()
        player:WaitForChild("PlayerGui")
        local saveData = Save:GetAsync(player.userId)
            for i = 1,#carts do
                Save:SetAsync(player.userId,carts[i],test[i])
                print("finished")
            end
        wait(3)
        if saveData then
            for i = 1,#saveData do
                print(saveData[i])
            end
        end 
    end)
end)


1 answer

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

If you want to save multiple values to a single DataStore, you should save them as a table:

carts = {"Red", "White", "Blue"}
test = {1,2,3}

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

game.Players.PlayerAdded:connect(function(player)
    pcall(function()
        player:WaitForChild("PlayerGui")
        Save:SetAsync(player.userId,{carts,test})
        wait(3)
    local saveData = Save:GetAsync(player.UserId)
        if saveData then
            for i = 1,#saveData[1] do
                print(saveData[i])  --> Red, White, Blue
            end
        for i = 1,#saveData[2] do
                print(saveData[i])  --> 1, 2, 3
            end
        end 
    end)
end)

I’ve also found that using pcall can sometimes not work when you try to print, so if it doesn’t work try removing that.

0
This is exactly what I've been looking for, thank you, I appreciate it. dirk2999 103 — 6y
Ad

Answer this question