Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

DataSave, why is this save returning nil?

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

✓ Accepted answer

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.

1 comment

0

This is exactly what I've been looking for, thank you, I appreciate it.

This discussion is preserved as it was. New answers, comments, and votes are closed.