2 Data stores in one game

Is it possible to have 2 data stores in one game? For example these are 2 server scripts and first one has this code:

local DSS = game:GetService("DataStoreService")
local DataA = DSS:GetDataStore("DataStoreA")

local function do_Stuff_with_datastoreA
    DataA:SetAsync(blah blah, false)
end

The second one has this code:

local DSS = game:GetService("DataStoreService")
local DataB = DSS:GetDataStore("DataStoreB")

local function do_Stuff_with_datastoreB
    DataB:SetAsync(blah blah, true)
end

If I insert this code somewhere in the second script, will it work or not:

game.Players.PlayerAdded:Connect(function(plr)
   if DataB:GetAsync(player.UserId) == true then
     print("do some stuff")
  end
end)

Will it print out “do some stuff” or can you not have 2 data stores i.e. DataStoreA & DataStoreB?

Have you tried doing this before?

No, because I am afraid it will mess up my data stores.

This should work perfectly fine I’m pretty sure.

Haven’t tried this myself before, but I can see it working perfectly fine. I usually just use a single DataStore, and save tables of information.

How can I use the single datastore/tables method?

local DSS = game:GetService("DataStoreService")
local DataA = DSS:GetDataStore("DataStoreA")

local Data = {Data1 = blah, blah, false;
Data2 = blah2, blah2, true;}
local function do_Stuff_with_datastoreA
    DataA:SetAsync(Data)
end

I think that’s how its done.

This is how its done in a normal data store script:

local DataStoreService = game:GetService("DataStoreService")
local DataStore1 = DataStoreService:GetDataStore("DataStore1") 

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local Points = Instance.new("IntValue")
	Points.Name = "Points"
	Points.Parent = leaderstats
	
	local Cash = Instance.new("IntValue")
	Cash.Name = "Cash"
	Cash.Parent = leaderstats
	
	
	local playerUserId = "Player_"..player.UserId
	
	-- Load Data
	
	local success, errormessage = pcall(function()
		data = DataStore1:GetAsync(playerUserId)
	end)
	
	if success then
		Points.Value = data.Points
		Cash.Value = data.Cash
	end
	
end)

game.Players.PlayerRemoving:Connect(function(player)
	local playerUserId = "Player_"..player.UserId
	
	local data = {Points = player.leaderstats.Points.Value;
		Cash = player.leaderstats.Cash.Value
	}
	
	
	local success, errormessage = pcall(function()
		DataStore1:SetAsync(playerUserId, data)
	end)
	
	
	if success then
		print("Data successfully saved")
	else
		print("There was a error when saving data")
		warn(errormessage)
	end

end)

1 Like

Yes.

And, wrap your calls in a pcall.

Yes, you can use 2 data stores. BUT, I recommend using one with a different “key”, here’s an example:
local datastore = game:GetService("DataStoreService"):GetDataStore("NameOfTheDatastore")
game.Players.PlayerAdded:Connect(function(plr)
datastore:SetAsync(plr.UserId .. "weapon", "someweaponname")
datastore:SetAsync(plr.UserId .. "name", plr.Name)
end
Hopefully that makes it less confusing for you :slight_smile:
Sorry for the lack of indents, I am new to devforum.

There are two way of doing it, one of them is already mentioned by others in here, Just combine both in a table and store.

You can also access two data stores in one script.

local DSS = game:GetService("DataStoreService")
local DataA = DSS:GetDataStore("DataStoreA")
local DataB = DSS:GetDataStore("DataStoreB")

local function do_Stuff_with_datastoreA
    DataA:SetAsync(blah blah, false)
end

local function do_Stuff_with_datastoreB
    DataB:SetAsync(blah blah, true)
end

And yes, it will print.

data can be nil if there is nothing saved, this will result in a error when setting the value, you can fix it by doing

if success and data then
end

or

Value = data.Points or 0 -- it will be 0 if the data is nil.

Not, really, if there is no data it will just be zero, not nil

No, the data WILL be nil if nothing is saved, think of it like doing

local Test
print(Test) -- nil

In regular data stores we use intvalues, which default value is 0:

Yes, the number is converted to 0 if the returned data is nil, a string or any bool-values.

However, the actual returned data is nil, when setting its value, it will automatically be set to 0 as explained above, as the returned data is not a valid number.

In cases where we are using StringValues or similar things, this method is highly recommended.

1 Like