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?
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
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)
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
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
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.