In my script I have around 13 datastores, is there anything bad that can come from that, and if so how could I improve on it?
local DataStore = game:GetService("DataStoreService")
local ds = DataStore:GetDataStore("RaceSave")
local RankData = DataStore:GetDataStore("SpecData")
local HumansEatenData = DataStore:GetDataStore("HumansEaten")
local ReiatsuTrainingData = DataStore:GetDataStore("ReiatsuTrainingData")
local MaskData = DataStore:GetDataStore("MaskData")
local HollowMaskTrainingData = DataStore:GetDataStore("HollowMaskTraining")
local MukenDataData = DataStore:GetDataStore("MukenData")
local SPData = DataStore:GetDataStore("SpiritPressureData")
local SpecialRankData = DataStore:GetDataStore("SpecialRankData")
local NameData = DataStore:GetDataStore("NameData1")
local ShikaiNameData = DataStore:GetDataStore("ShikaiNameData1")
local ShikaiCallData = DataStore:GetDataStore("ShikaiCallData")
local ShikaiUnlockedData = DataStore:GetDataStore("ShikaiUnlockedData")
you shouldnât be using that much datastores, itâs not efficient as it will lead to multiple datastore calls and i believe thereâs a quota on that
instead, create a player data structure with a table and save in only a single datastore
tables can be stored and loaded just like any other type of value.
-- Loading Data
game.Players.PlayerAdded:Connect(function(plr)
-- Example DataStore
local key = plr.UserId
local success, data = pcall(function()
return DataStore:GetAsync(key)
end)
-- Accessing variable.
local RankData = data.Rank
end)
-- Saving Data
game.Players.PlayerRemoving:Connect(function(plr)
local key = plr.UserId
local data
for _, value in pairs(plr.leaderstats:GetChildren()) do
table.insert(data,value.Value)
end
local success,err = pcall(function()
DataStore:SetAsync(key,data)
end)
end)
local HttpService = game:GetService("HttpService")
local ShikaiUnlockedData = DataStore:GetDataStore("ShikaiUnlockedData")
local Data = {
Key1 = "Value",
Key2 = 123
}
-- Example of saving a table
local serialized = HttpService:JSONEncode(Data)
ShikaiUnlockedData:SetAsync('PlayerKey',serialized)
-- Now we get the data
local serialized_data = ShikaiUnlockedData:GetAsync('PlayerKey')
Data = HttpService:JSONDecode(serialized_data)
you can substitute âPlayerKeyâ for the playerâs UserId
When I load the data I put it as a value under the player in a folder, when they leave I save it so how would I save it back to the table from the value
i.e when I leave how do I change shikai name in the data to âTestâ instead of âLockedâ
local Data = {
["Unlocked"] = "NotUnlocked",
["Call"] = "Locked",
["ShikaiName"] = "Locked",
["WhatSword"] = "None",
}
ShikaiName.Changed:Connect(function()
Data.ShikaiName = ShikaiName.Value
local serialized = HttpService:JSONEncode(Data)
ShikaiUnlockedData:SetAsync(player.UserId,serialized)
end)
try to avoid setting the datastore multiple times unless you really need to
it would be best to either periodically save the data (5 - 10 minutes) or save the data when the player leaves