Hi guys :) I am trying to save leaderstats every 5 seconds! :) Any ideas why is this not working? (I am not great with datastores :/ ) Thanks in advance!
NOTE: Currently, I am saving when the player leaves the game, but it doesn't save if the game crashes. I need to get over this somehow, either by saving the data when it changes and when they leave or every 5 seconds. Thanks.
local ds = game:GetService("DataStoreService"):GetDataStore("stats")
lolz={"Escapes","Coins","Diamonds"}
game.Players.PlayerAdded:connect(function(plyr)
local a=Instance.new("NumberValue")
a.Parent=plyr
a.Name="leaderstats"
for i=1,#lolz do
local stat=Instance.new("NumberValue")
stat.Parent=a
stat.Value=0
stat.Name=lolz[i]
end
local child=plyr.leaderstats:GetChildren()
for i=1, #child do
child[i].Value=ds:GetAsync(plyr.userId..child[i].Name)
end
end)
while wait(5) do
local child=plyr.leaderstats:GetChildren()
for i=1, #child do
child[i].Value=ds:SetAsync(plyr.userId..child[i].Name,child[i].Value)
end
game.Players.PlayerRemoving:connect(function(plyr)
local child=plyr.leaderstats:GetChildren()
for i=1, #child do
child[i].Value=ds:SetAsync(plyr.userId..child[i].Name,child[i].Value)
end
end)

6 comments
Saving every 5 seconds is an excessive amount of requests for ROBLOX.
^
Just save when someone leaves the game
^
@LordDragonZord but would that save the Player's Data if they crash though?
^^ It would not save if the game crashes, which is why I want to save it every 5 seconds or save it when the leaderstat changes!