Help with Datastore rables?

So, im trying to make a datastore system table where you can have many passives, or just one. However, I don’t want to make a bool value for every single one. Is there a way to datastore an actual table instead of storing every single stat 1 by 1?

Same with guilds, but i want it to be a global datastore, which i don’t know how to make

this is me trying to do it so far:

local DS = game:GetService("DataStoreService"):GetDataStore("DataStore")
game.StarterPlayer.EnableMouseLockOption = false

game.Players.PlayerAdded:Connect(function(plr)
	local class = Instance.new("StringValue", plr)
	class.Name = "Passives" 
	class.Value = ""
	
	local UsedGuild = {} 
	
	local firstJoin = Instance.new("BoolValue", plr)
	firstJoin.Name = "FirstJoin"
	firstJoin.Value = true 
	
	local data;
	local success, err = pcall(function()
		data = DS:GetAsync("YourKey"..plr.UserId)
	end)
	if data then
		-- it is not a new player --
		firstJoin.Value = data.FirstJoin
		currency.Value = data.Passives
		UsedGuild = data.Guild
	else
	print("new plr")
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local data = {
		FirstJoin = plr.FirstJoin.Value,
		Guild = UsedGuild,
		Passives = plr.leaderstats.Passives.Value
	}
	local success, err = pcall(function()
		DS:SetAsync("DataKey"..plr.UserId, data)
	end)
end)
local DS = game:GetService("DataStoreService"):GetDataStore("DataStore")
-- game.StarterPlayer.EnableMouseLockOption = false // this isn't scriptable. Do it from the explorer window

game.Players.PlayerAdded:Connect(function(plr)
	local class = Instance.new("StringValue", plr)
	class.Name = "Passives" 
	class.Value = ""
	
	local UsedGuild = {} 
	
	local firstJoin = Instance.new("BoolValue", plr)
	firstJoin.Name = "FirstJoin"
	firstJoin.Value = true 
	
	local success, err = pcall(function()
		return DS:GetAsync("YourKey"..plr.UserId)
	end)

	if success and data then -- check if it was able to get the data too, otherwise you'd deal with errors
		-- it is not a new player --
		firstJoin.Value = data.FirstJoin
		currency.Value = data.Passives
		UsedGuild = data.Guild
	else
	    print("new plr")
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local data = {
		FirstJoin = plr.FirstJoin.Value,
		Guild = UsedGuild or {}, -- what's "UsedGuild" ? it's only defined in the PlayerAdded event
		Passives = plr.leaderstats.Passives.Value
	}
	local success, err = pcall(function()
		DS:SetAsync("YourKey"..plr.UserId, data) -- these keys were not the same
	end)

    if not success then
       warn(err)
    end
end)


FYI, DataStores only work properly in live games, not studio

If you want to, you can look into these:

You can switch between Strings and Tables, so hopefully this helps!

Hey there!

I have actually found out a way how to test DataStores correctly while you are in Studio.
If you are interested, then you can read my topic, where I shared the way that I found it is possible. It works fine for testing DataStores for me.

1 Like

so basically, I want to datastore a table, and i dont know how to do that.

is it possible to save all children of a folder using datastore?

This is very possible, all you have to do is serialize your code.

Here’s a start:

function Serialize(Folder)
    local Schema = {};

    for _, Child in pairs(Folder:GetChildren()) do
        if Child:IsA("ValueBase") then
            Schema[Child.Name] = Child.Value
        elseif next(Child:GetChildren()) then
            Schema[Child.Name] = Serialize(Child);
        end
    end

    return Schema
end
1 Like

Would this be a valid option?

local DS = game:GetService("DataStoreService"):GetDataStore("YourDataStore")
game.StarterPlayer.EnableMouseLockOption = false

game.Players.PlayerAdded:Connect(function(plr)
	local passivefolder = Instance.new("Folder",plr)
	passivefolder.Name = "PassiveFolder"
	local passives = {}
	
	local data;
	local success, err = pcall(function()
		data = DS:GetAsync("YourKey"..plr.UserId)
	end)
	if data then
		-- it is not a new player --
		passives = data.passiveData
		local allpassivesfolder = game.ReplicatedStorage:WaitForChild("Passives")
		for i, v in pairs(passives) do
			allpassivesfolder:FindFirstChild(v)
		end
	else
		-- it is a new player --
		print("Welcome "..plr.Name.."!")
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local passives = {}
	for i, v in pairs(plr.PassiveFolder:GetChildren()) do
		table.insert(passives,v.Name)
	end
	local data = {
		passiveData = passives
	}
	local success, err = pcall(function()
		DS:SetAsync("YourKey"..plr.UserId, data)
	end)
end)