So, I asked a question a day or two ago, about my DataStore not working. Well, it works now, but only occasionally.
I'm not sure what to do, because I don't wanna completely break it again, so I came here. If anyone can help, I'm new to scripting so it would be appreciated. Here's my code:
local dataStoreService = game:GetService("DataStoreService")
local StatStore = dataStoreService:GetDataStore("DataStats")
game.Players.PlayerAdded:Connect(function(player)
game.Workspace:WaitForChild(player.Name)
player:WaitForChild("leaderstats")
player.PlayerGui.Codes.SpeedBoost.Parent = player
local Key = player.UserId
local Leaderstats = player.leaderstats
local Steps = Leaderstats.Steps
local SpeedBoost = player.SpeedBoost
local DefaultSteps = 0
local DefaultData = {
0,
false
}
local Stats = {
Steps.Value,
SpeedBoost.Value
}
local NewStats
local recieved, notRecieved = pcall(function()
NewStats = StatStore:GetAsync(Key)
end)
if recieved then
Steps.Value = NewStats[1]
SpeedBoost.Value = NewStats[2]
print("Stats were recieved")
wait(1)
StatStore:SetAsync(Key, Stats)
else
StatStore:SetAsync(Key, DefaultData)
print("Stats were not recieved")
warn(notRecieved)
end
wait(0.1)
while wait(10) do
local saved, notSaved = pcall(function()
StatStore:UpdateAsync(Key, function(oldValue)
return Stats
end)
end)
if saved then
print("Stats were auto saved!")
else
print("Stats didn't auto save.")
end
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local Key = player.UserId
local Leaderstats = player.leaderstats
local Steps = Leaderstats.Steps
local SpeedBoost = player.SpeedBoost
local DefaultData = {
0,
false
}
local Stats = {
Steps.Value,
SpeedBoost.Value
}
local success, result = pcall(function()
StatStore:UpdateAsync(Key, function(oldValue)
return Stats
end)
end)
if success then
print("Stats were saved!")
else
print("Stats were not saved.")
warn(result)
end
end)
Sorry if it's messy, like I said, I'm new to scripting.
Steps is an IntValue and SpeedBoost is a BoolValue.

2 comments
I knew it was too often, I only had it at 10 for testing purposes. But I'll try changing it anyway, thanks.
It seems to work every time now, I can't believe I didn't think to change that. Thanks again!