Hey guys, I’m back with a new question, I making a game, and I’m saving 4 differents keys when the player leave the game.
My script is perfectly working, I have 4 tables, one for Values, one for pets, one for hats and one for tools.
The problem is, I don’t know if it can make problems to save 4 keys at the same time.
So I’m trying to put all my tables into one table and this is how my script look likes for now.
---- Services ----
local SS = game:GetService("ServerStorage")
local DSS = game:GetService("DataStoreService")
local DS1 = DSS:GetDataStore("DataStore1")
---- On Connect Function ----
game.Players.PlayerAdded:Connect(function(plr)
local plrInf = Instance.new("Folder",plr)
plrInf.Name = "leaderstats"
local firstC = Instance.new("IntValue",plrInf)
firstC.Name = "Coins"
local level = Instance.new("IntValue",plrInf)
level.Name = "Level"
local exp = Instance.new("IntValue",plrInf)
exp.Name = "Exp"
local Inv = Instance.new("Folder",plr)
Inv.Name = "Inventory"
local ToolsInv = Instance.new("Folder",Inv)
ToolsInv.Name = "ToolsInventory"
local PetsInv = Instance.new("Folder",Inv)
PetsInv.Name = "PetsInventory"
local HatsInv = Instance.new("Folder",Inv)
HatsInv.Name = "HatsInventory"
local tdebounce = Instance.new("BoolValue",SS.DebFolder)
tdebounce.Name = "Deb-"..plr.Name
local data, VData, TData, HData, PData
local success, errormessage = pcall(function()
data = DS1:GetAsync("Data-"..plr.UserId)
VData = data.VData
TData = data.toolsSave
HData = data.hatsSave
PData = data.petsSave
end)
if success then
if VData ~= nil then
firstC.Value = VData.coins
level.Value = VData.level
exp.Value = VData.exp
else
firstC.Value = 0
level.Value = 1
exp.Value = 0
end
if TData ~= nil then
for _, tool in pairs(TData) do
local nTool = SS.Tools:FindFirstChild(tool)
if tool and nTool then
local newTool = nTool:Clone()
newTool.Parent = ToolsInv
end
end
end
if HData ~= nil then
for _, hat in pairs(HData) do
local nHat = SS.Hats:FindFirstChild(hat)
if hat and nHat then
local newHat = nHat:Clone()
newHat.Parent = HatsInv
end
end
end
if PData ~= nil then
for _, pet in pairs(PData) do
local nPet = SS.Pets:FindFirstChild(pet)
if pet and nPet then
local newPet = nPet:Clone()
newPet.Parent = PetsInv
end
end
end
end
end)
---- On Disconnect Function ----
game.Players.PlayerRemoving:Connect(function(plr)
local SaveData = {}
local VSave = {
coins = plr.leaderstats.Coins.Value;
level = plr.leaderstats.Level.Value;
exp = plr.leaderstats.Exp.Value
}
local toolsSave = {}
for _, tool in pairs(plr.Inventory.ToolsInventory:GetChildren()) do
local nTool = SS.Tools:FindFirstChild(tool.Name)
if tool and nTool then
table.insert(toolsSave,tool.Name)
end
end
local hatsSave = {}
for _, hat in pairs(plr.Inventory.HatsInventory:GetChildren()) do
local nHat = SS.Hats:FindFirstChild(hat.Name)
if hat and nHat then
table.insert(hatsSave,hat.Name)
end
end
local petsSave = {}
for _, pet in pairs(plr.Inventory.PetsInventory:GetChildren()) do
local nPet = SS.Pets:FindFirstChild(pet.Name)
if pet and nPet then
table.insert(petsSave,pet.Name)
end
end
table.insert(SaveData,VSave)
table.insert(SaveData,toolsSave)
table.insert(SaveData,hatsSave)
table.insert(SaveData,petsSave)
local success, errormessage = pcall(function()
DS1:SetAsync("Data-"..plr.UserId,SaveData)
end)
end)
game:BindToClose(function()
for _, plr in pairs(game.Players:GetPlayers()) do
local SaveData = {}
local VSave = {
coins = plr.leaderstats.Coins.Value;
level = plr.leaderstats.Level.Value;
exp = plr.leaderstats.Exp.Value
}
local toolsSave = {}
for _, tool in pairs(plr.Inventory.ToolsInventory:GetChildren()) do
local nTool = SS.Tools:FindFirstChild(tool.Name)
if tool and nTool then
table.insert(toolsSave,tool.Name)
end
end
local hatsSave = {}
for _, hat in pairs(plr.Inventory.HatsInventory:GetChildren()) do
local nHat = SS.Hats:FindFirstChild(hat.Name)
if hat and nHat then
table.insert(hatsSave,hat.Name)
end
end
local petsSave = {}
for _, pet in pairs(plr.Inventory.PetsInventory:GetChildren()) do
local nPet = SS.Pets:FindFirstChild(pet.Name)
if pet and nPet then
table.insert(petsSave,pet.Name)
end
end
table.insert(SaveData,VSave)
table.insert(SaveData,toolsSave)
table.insert(SaveData,hatsSave)
table.insert(SaveData,petsSave)
local success, errormessage = pcall(function()
DS1:SetAsync("Data-"..plr.UserId,SaveData)
end)
end
end)