How to integrate a list to datastore

Hello, what i mean with the title is basically this:

local list {
        waluigi = false;
        wario = true;
        luigi = false;
        mario = false;
        bowser = true;
}

And save that in a datastore.

If that wasn’t understandable, basically i want to make a list to save a datastore, more specifically for all the items in a shop, and the bool values if the player owns them.

If anyone could help, that would be so good, thanks!

1 Like

I think you mean table. All you have to do is do

datastore:SetAsync(Key, list/table)
1 Like

I mean a table would work too, but would i set true or false values with it too?

I don’t know, i don’t use tables very often.

The example you put above would work fine. You dont need to do anything else other than put the table. It’ll save all its contents.

Ok, but is the way i’m making the table correct? Like in the example

local list = {
	waluigi = false;
	wario = true;
}

yes, that’ll work fine. Just make sure you call the function I sad above.

If i want to get a specific value in the table, how would i do so? For example if i want to know if “waluigi” is true or false. (This is for other stuff related to this code of course)

do:

list.waluigi

it’ll get it’s value

2 Likes

Ok. Also when you say

Is DataStore DataStoreService or the DataStore of the script?

And what is Key?

Also i’m sorry for making you take all this time to help with this.

THE DATASTORE OF THE SCRIPT. Not data store serivce

Ok, and what is the “Key” you mentioned in the script

Its confusing to explain. Look here Data Stores | Roblox Creator Documentation

local DatastoreService = game:GetService("DataStoreService")

local DatastoreName = "DatastoreNameTest1"
local Datastore = DatastoreService:GetDataStore(DatastoreName)

local function SaveList(Player, List)
	 --[[
	 	The 'List' parameter should be a table/array, like this:
	 		local list = {
        		waluigi = false;
        		wario = true;
        		luigi = false;
        		mario = false;
        		bowser = true;
			}
			
		The 'Player' parameter would be equal to the player who's owned items you are saving
	 ]]
	if Player and Player:IsA("Player") then -- Checking if the Player and List are the correct things
		if List and typeof(List) == "table" then
			local Key = "Player_"..tostring(Player.UserId) -- The key, which will be used to save the list for the player
			local isSuccess = pcall(function()
				Datastore:SetAsync(Key, List)
			end)
		end
	end
end

local function GetList(Player)
	 --[[
		The 'Player' parameter would be equal to the player who's owned items you are getting/reading
	 ]]
	if Player and Player:IsA("Player") then -- Checking if the Player and List are the correct things
		local Key = "Player_"..tostring(Player.UserId) -- The key, which will be used to retrieve the list for the player
		local Response
		local isSuccess = pcall(function()
			Response = Datastore:GetAsync(Key)
		end)
		if isSuccess and Response ~= nil then
			if typeof(Response) == "table" then
				return Response
			end
		end
	end
end

--[[
What the functions return:

	 SaveList - nothing/nil
	 
	 GetList - The requested list/nil (if not saved yet)
]]
2 Likes