Hello, this code saves players data in datastore 1 out of 3 times and idk why.
I think that it is slow and dont have much timie to record data tables in datastore when players leaves the game.
Any suggestions?
local function CreateStatsTable(plr)
local player_stats = {}
for i,v in pairs(plr.StatsFolder:GetChildren()) do
player_stats[v.Name] = v.Value
end
return player_stats
end
local function CreateLeaderTable(plr)
local player_stats = {}
for i,v in pairs(plr.leaderstats:GetChildren()) do
player_stats[v.Name] = v.Value
end
return player_stats
end
local function CreateJutsuTable(plr)
local player_stats = {}
for i,v in pairs(plr.JutsuUnlocked:GetChildren()) do
player_stats[v.Name] = v.Value
end
return player_stats
end
local function CreateModTable(plr)
local player_stats = {}
for i,v in pairs(plr.ModsUnlocked:GetChildren()) do
player_stats[v.Name] = v.Value
end
return player_stats
end
game.Players.PlayerRemoving:Connect(function(plr)
local player_stats = CreateStatsTable(plr)
local player_leaderstats = CreateLeaderTable(plr)
local player_Jutsu = CreateJutsuTable(plr)
local player_Mod = CreateModTable(plr)
local success, err = pcall(function()
local StatsUserId = "StatsFolder_"..plr.UserId
GameDs:SetAsync(StatsUserId, player_stats)
local LeaderPlayerUserId = "LeaderstatsFolder_"..plr.UserId
GameDs:SetAsync(LeaderPlayerUserId, player_leaderstats)
local JutsuUserId = "JutsuFolder_"..plr.UserId
GameDs:SetAsync(JutsuUserId, player_Jutsu)
local ModUserId = "ModFolder_"..plr.UserId
GameDs:SetAsync(ModUserId, player_Mod)
end)
if not success or err then
warn("Could not save data!")
print(err)
end
end)```