I’ve been trying for several months to create a DataStore that won’t randomly fail, yet it seems like it’s impossible. Why?
Randomly peoples stats will reset or they’ll lose some of their credits (in-game currency) with out spending them.
I tried to revise the code but nothing seems to be work! I really need any advice you guys can give me because credits are important to the players and a couple of them have been getting mad at me & the game!
Here’s my leaderstat & DataStore system:
local DataBaseStore = game:GetService("DataStoreService"):GetDataStore("Saved")
local StatSholder = script.Stats
game.Players.PlayerAdded:Connect(function(NewPlayer)
local Key = "Player-ID:" .. NewPlayer.userId
local GetSave = DataBaseStore:GetAsync(Key)
local DataBaseFolder = Instance.new("Folder", NewPlayer)
DataBaseFolder.Name = "leaderstats"
for i, Stats in pairs(StatSholder:GetChildren())do
local NewStats = Instance.new(Stats.ClassName)
NewStats.Name = Stats.Name
NewStats.Value = Stats.Value
NewStats.Parent = DataBaseFolder
end
if GetSave then
for i, Stats in pairs(script.Stats:GetChildren())do
DataBaseFolder[Stats.Name].Value = GetSave[i]
print (i, Stats.Name, Stats.Value)
end
else
local StatsToSave = {}
for i, Stats in pairs(DataBaseFolder:GetChildren()) do
table.insert(StatsToSave, Stats.Value)
print(i, Stats.Name, Stats.Value)
end
DataBaseStore:SetAsync(Key, StatsToSave)
end
while wait(30) do
local StatsToSave = {}
for i, Stats in pairs(DataBaseFolder:GetChildren()) do
table.insert(StatsToSave, Stats.Value)
print(i, Stats.Name, Stats.Value)
end
DataBaseStore:SetAsync(Key, StatsToSave)
end
game.Players.PlayerRemoving:Connect(function(OldPlayer)
local Key = "Player-ID" .. OldPlayer.userId
local StatsToSave = {}
for i, Stats in pairs(DataBaseFolder:GetChildren()) do
table.insert(StatsToSave, Stats.Value)
print(i, Stats.Name, Stats.Value)
end
DataBaseStore:SetAsync(Key, StatsToSave)
end)
end)
More Info:
There’s a folder in the script named “Stats” and inside that folder there is a NumberValue named “Credits” (Not sure if this information would be helpful or not!)