DataStore2 and ProfileService. Confused

Hi,
I’ve been looking around on how to use DataStore2 and ProfileService, but I can’t seem to understand. I get the basics, but could someone break it down for me? Thanks!

Ok so there is not really a massive difference, they are both data store modules which do the same thing essentially. There is not much difference between the two modules, they both are designed to prevent data loss.

I believe that profile service supports ordered data stores whereas data store 2 doesn’t.

Also data store 2 is a caching module whereas profile service saves the data and wont cache it. For this matter I strongly believe that data store 2 is better. However on the flip side nowadays I just make my own data stores because not much can go wrong if you use pcalls or Protected Calls, These videos explain how to use and the benefits of the modules:

Profile Service:

Data store 2:

My personal preference is data store 2 but overall i think they are both great modules but I just use my own data solution for my games.

1 Like

First of all, you have to choose one of them - either DataStore2 or ProfileService. I disagree with the guy above me saying datastore2 is better. as he mentioned, not much can go wrong if you use pcall and be a bit careful when saving data, so caching is not that necessary. ProfileService on the other hand offers session locking which basically prevents any item duplication bug/glitch and that’s really helpful. It is still a personal preference at the end of the day. As for the resources, if you’re a beginner, then the tutorials linked above will help, otherwise, I’d recommend reading the documentation.

So I’m testing out saving data to tables instead of using modules, as I have lots of data to save. Would the following script work?:

local MasterStore = DSS:GetDataStore("MasterStore")

game.Players.PlayerAdded:Connect(function(plr)
	
	local Data = MasterStore:GetAsync(plr.UserId)
	
	local NumValue = 1
	
	local Stat = Instance.new("Folder")
	Stat.Parent = plr
	Stat.Name = "Stats"
	
	local pinfo = Instance.new("Folder")
	pinfo.Name = "PartyInfo"
	pinfo.Parent = Stat
	
	local teinfo = Instance.new("ObjectValue")
	teinfo.Name = "TeamInfo"
	teinfo.Parent = pinfo
	
	local plinfo = Instance.new("ObjectValue")
	plinfo.Parent = pinfo
	plinfo.Name = "PlayerInfo"
	
	local t = Instance.new("Folder")
	t.Name = "Tools"
	t.Parent = Stat
	
	local o = Instance.new("Folder")
	o.Name = "Owned"
	o.Parent = Stat
	
	local enchs = Instance.new("Folder")
	enchs.Parent = Stat
	enchs.Name = "Enchantments"
	
	local orbs = Instance.new("IntValue")
	orbs.Name = "EnchantingOrbs"
	orbs.Parent = Stat
	orbs.Value = 0
	local num = Instance.new("IntValue")
	num.Parent = orbs
	num.Name = "Num"
	num.Value = NumValue
	NumValue = NumValue + 1
	if Data ~= nil then
		if Data[num.Value] ~= nil then
			num.Parent.Value = Data[num.Value]
		end		
	end
	
	local gold = Instance.new("IntValue")
	gold.Parent = Stat
	gold.Name = "Gold"
	gold.Value = 0
	local num = Instance.new("IntValue")
	num.Parent = gold
	num.Name = "Num"
	num.Value = NumValue
	NumValue = NumValue + 1
	if Data ~= nil then
		if Data[num.Value] ~= nil then
			num.Parent.Value = Data[num.Value]
		end		
	end
	
	local loot = Instance.new("IntValue")
	loot.Parent = Stat
	loot.Name = "Loot"
	loot.Value = 0
	local num = Instance.new("IntValue")
	num.Parent = loot
	num.Name = "Num"
	num.Value = NumValue
	NumValue = NumValue + 1
	if Data ~= nil then
		if Data[num.Value] ~= nil then
			num.Parent.Value = Data[num.Value]
		end		
	end
	
	local pick = Instance.new("StringValue")
	pick.Name = "Pickaxe"
	pick.Parent = t
	pick.Value = "None"
	local num = Instance.new("IntValue")
	num.Parent = pick
	num.Name = "Num"
	num.Value = NumValue
	NumValue = NumValue + 1
	if Data ~= nil then
		if Data[num.Value] ~= nil then
			num.Parent.Value = Data[num.Value]
		end		
	end
	
	local sword = Instance.new("StringValue")
	sword.Name = "Sword"
	sword.Parent = t
	sword.Value = "None"
	local num = Instance.new("IntValue")
	num.Parent = sword
	num.Name = "Num"
	num.Value = NumValue
	NumValue = NumValue + 1
	if Data ~= nil then
		if Data[num.Value] ~= nil then
			num.Parent.Value = Data[num.Value]
		end		
	end
	
	local armor = Instance.new("StringValue")
	armor.Name = "Armor"
	armor.Parent = t
	armor.Value = "None"
	local num = Instance.new("IntValue")
	num.Parent = armor
	num.Name = "Num"
	num.Value = NumValue
	NumValue = NumValue + 1
	if Data ~= nil then
		if Data[num.Value] ~= nil then
			num.Parent.Value = Data[num.Value]
		end		
	end
	
	local ench = Instance.new("StringValue")
	ench.Name = "Enchantment"
	ench.Parent = Stat
	ench.Value = "None"
	local num = Instance.new("IntValue")
	num.Parent = ench
	num.Name = "Num"
	num.Value = NumValue
	NumValue = NumValue + 1
	if Data ~= nil then
		if Data[num.Value] ~= nil then
			num.Parent.Value = Data[num.Value]
		end		
	end
	
	for _, e in pairs(game.ReplicatedStorage.Modules.SwordModules:GetChildren()) do
		local v = Instance.new("BoolValue")
		v.Parent = o
		v.Name = e.Name
		v.Value = false
		local num = Instance.new("IntValue")
		num.Parent = v
		num.Name = "Num"
		num.Value = NumValue
		NumValue = NumValue + 1
		if Data ~= nil then
			if Data[num.Value] ~= nil then
				num.Parent.Value = Data[num.Value]
			end		
		end
	end
	for _, e in pairs(game.ReplicatedStorage.Modules.ArmorModules:GetChildren()) do
		local v = Instance.new("BoolValue")
		v.Parent = o
		v.Name = e.Name.."Armor"
		v.Value = false
		local num = Instance.new("IntValue")
		num.Parent = v
		num.Name = "Num"
		num.Value = NumValue
		NumValue = NumValue + 1
		if Data ~= nil then
			if Data[num.Value] ~= nil then
				num.Parent.Value = Data[num.Value]
			end		
		end
	end
	for _, e in pairs(game.ReplicatedStorage.Modules.PickaxeModules:GetChildren()) do
		local v = Instance.new("BoolValue")
		v.Parent = o
		v.Name = e.Name
		v.Value = false
		local num = Instance.new("IntValue")
		num.Parent = v
		num.Name = "Num"
		num.Value = NumValue
		NumValue = NumValue + 1
		if Data ~= nil then
			if Data[num.Value] ~= nil then
				num.Parent.Value = Data[num.Value]
			end		
		end
	end
	for _, e in pairs(game.ReplicatedStorage.Modules.Enchants:GetChildren()) do
		local v = Instance.new("BoolValue")
		v.Parent = enchs
		v.Name = e.Name
		v.Value = false
		local num = Instance.new("IntValue")
		num.Parent = v
		num.Name = "Num"
		num.Value = NumValue
		NumValue = NumValue + 1
		if Data ~= nil then
			if Data[num.Value] ~= nil then
				num.Parent.Value = Data[num.Value]
			end		
		end
	end
	
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local DataTable = {
		
	}
	
	for _, e in pairs(plr.Stats:GetDescendants()) do
		if e:IsA("StringValue") or e:IsA("IntValue") or e:IsA("BoolValue") then
			table.insert(DataTable, e.Num.Value, e.Value)
		end
	end
	
	MasterStore:SetAsync(plr.UserId, DataTable)
end)

From glancing over it it seems like it would work, however I cannot guarantee it will work. If it doesn’t work I can provide you with my table saving code.

1 Like

Code I use:

Module script inside of server storage

-- Module script inside ServerStorage

local LeaderstatsModule = {}

LeaderstatsModule.CreateStat = function(player)
     
    -- Create all the relevent items 

    local leaderstats = Instance.new("Folder", player)
    leaderstats.Name = "leaderstats"

    local Cash = Instance.new("IntValue", leaderstats)
    Cash.Name = "Cash"

    local MerchSold = Instance.new("IntValue", leaderstats)
    MerchSold.Name = "Coins"

end

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("StatsDataStore")

LeaderstatsModule.LoadSavedData = function(player)

    local Key = player.UserId.."-Data"
    local Data
    local success, err = pcall(function()
        Data = DataStore:GetAsync(Key)
    end)
    
    -- Load the data in the order you created the item like below:
    if Data ~= nil then
        player.leaderstats.Cash.Value = Data["Cash"]
        player.leaderstats.Coins.Value = Data["MerchSold"]
        print("Data Loaded")
    else
        player.leaderstats.Cash.Value = 1000
        player.leaderstats.Coins.Value = 0
        print("Failed To Load Data or New Player")
    end

    if not success then
		warn("Could not save data, error:"..tostring(err))
	end

end

function CreateTable(player) -- Creates the table
    local playerStats = {}
	for _, stat in pairs(player.leaderstats:GetChildren()) do
		playerStats[stat.Name] = stat.Value
	end
    return playerStats
end

LeaderstatsModule.SaveData = function(player) -- Saves the table created above 
    local playerStats = CreateTable(player)
	local success, err = pcall(function()
		local Key = player.UserId.."-Data"
		DataStore:SetAsync(Key, playerStats)
	end)
	
	if not success then
		warn("Could not save data, error:"..tostring(err))
    else 
        print("Data Saved For: "..player.UserId)
	end

end

return LeaderstatsModule

Server script inside of server script service:

local ServerStorage = game:GetService("ServerStorage")
local Module = require(ServerStorage:WaitForChild("LeaderstatsModule") -- Name of the Module 

local function OnPlayerJoin(player)
    Module.Create(player)
    Module.LoadSavedData(player)
end

local function OnPlayerLeave(player)
    Module.SaveData(player)
end

game.Players.PlayerAdded:Connect(OnPlayerJoin)
game.Players.PlayerRemoving:Connect(OnPlayerExit)

You can simply keep adding data to save to the table or if you need to save two or more sperate values just create more tables and save them to different keys. I am making a tutorial in community resources on saving data in tables so I can link that once it is done. Hope this helps, apoaddda

3 Likes

Thanks for your module. I will see if my code works, and test its limits. I may or may not use your module depending on how well my code works.

So my code is not working, same code as above, no errors in output. Could you assist me?