I made a data store code for the game, you guys think this enough good ?
DataStore = game:GetService("DataStoreService")
PlayerStats = DataStore:GetDataStore("PlayerData")
local StarterStats = {
Power = 0;
Test = 0;
}
local StarterSkill = {
Skill1 = "";
Skill2 = "";
Skill3 = "";
Skill4 = "";
Skill5 = "";
Skill6 = "";
Skill7 = "";
SkillR = "";
SkillT = "";
}
function GetTable(folder)
local tableV = {};
for _, stat in pairs(folder:GetChildren()) do
tableV[stat.Name] = stat.Value;
end
return tableV
end
local function CreateStarter(plr)
local folder = Instance.new("Folder",plr)
folder.Name = "Stats"
for name,val in pairs(StarterStats) do
local valtype = typeof(val)
if valtype == "string" then
local instance = Instance.new("StringValue",folder)
instance.Name = name
instance.Value = val
elseif valtype == "number" then
local instance = Instance.new("NumberValue",folder)
instance.Name = name
instance.Value = val
elseif valtype == "boolean" then
local instance = Instance.new("BoolValue",folder)
instance.Name = name
instance.Value = val
end
end
end
local function CreateSkills(plr)
local folder = Instance.new("Folder",plr)
folder.Name = "Skills"
for name,val in pairs(StarterSkill) do
local instance = Instance.new("StringValue",folder)
instance.Name = name
instance.Value = val
end
end
local function CreateValues(folder,dataTable)
for name,val in pairs(dataTable) do
local valtype = typeof(val)
if valtype == "string" then
local instance = Instance.new("StringValue",folder)
instance.Name = name
instance.Value = val
elseif valtype == "number" then
local instance = Instance.new("NumberValue",folder)
instance.Name = name
instance.Value = val
elseif valtype == "boolean" then
local instance = Instance.new("BoolValue",folder)
instance.Name = name
instance.Value = val
end
end
print(folder.Name)
end
local function createAnotherFolder(plr)
local folder = Instance.new("Folder",plr)
folder.Name = "MeleeSkills"
local folder = Instance.new("Folder",plr)
folder.Name = "KiSkills"
local folder = Instance.new("Folder",plr)
folder.Name = "Transformatios"
end
local function CreateData(plr)
local stat = {};
local table1 = GetTable(plr.Stats)
local table2 = GetTable(plr.Skills)
local table3 = GetTable(plr.MeleeSkills)
local table4 = GetTable(plr.KiSkills)
local table5 = GetTable(plr.Transformatios)
stat = {table1,table2,table3,table4,table5}
local Succes,Error = pcall(function()
PlayerStats:GetAsync(plr.UserId)
end)
if Succes then
print("data loaded :3")
else
PlayerStats:SetAsync(plr.UserId,stat)
end
end
local function LoadData(player)
local statData = {};
local Succes,Error = pcall(function()
statData = PlayerStats:GetAsync(player.UserId)
end)
if statData then
print("ı loading your datas")
function Loader(folder,dataTable)
for name, val in pairs(dataTable) do
folder[name].Value = val;
end
print(folder.Name)
end
for val,n in pairs(statData) do
if val == 1 then
Loader(player.Stats,n)
elseif val == 2 then
Loader(player.Skills,n)
elseif val == 3 then
CreateValues(player.MeleeSkills,n)
elseif val == 4 then
CreateValues(player.KiSkills,n)
elseif val == 5 then
CreateValues(player.Transformatios,n)
end
end
print("data load completed for "..player.Name)
else
print("New Data for ".. player.Name.. " :^3");
CreateData(player)
end
end
local function NewDataSave(player)--data saver
local stat = {};
local table1 = GetTable(player.Stats)
local table2 = GetTable(player.Skills)
local table3 = GetTable(player.MeleeSkills)
local table4 = GetTable(player.KiSkills)
local table5 = GetTable(player.Transformatios)
stat = {table1,table2,table3,table4,table5}
local Succes,Error = pcall(function()
PlayerStats:SetAsync(player.UserId,stat)
end)
if Succes then
print("Datas Saved For "..player.Name)
else
print("Datas Not Saved oof")
end
end
game.Players.PlayerAdded:Connect(function(plr)
CreateStarter(plr)
CreateSkills(plr)
createAnotherFolder(plr)
LoadData(plr)
end)
game.Players.PlayerRemoving:Connect(function(plr)
NewDataSave(plr)
end)