Firstly I have no idea how to make my DataStore save player data in timed intervals, and secondly I don’t know what the time limit is to saving data(I think its 60 seconds but I’m not sure.) This is my DataStore :
local ClickDataStore = DataStoreService:GetDataStore("ClickDataStore")
game.Players.PlayerAdded:Connect(function(player)
local stats = Instance.new("Folder")
stats.Name = "leaderstats"
stats.Parent = player
local Clicks = Instance.new("IntValue")
Clicks.Name = "Clicks"
Clicks.Parent = player.leaderstats
local data
local success, errormessage = pcall(function()
data = ClickDataStore:GetAsync(player.userId.."Clicks")
end)
if success then
Clicks.Value = data
print("Player Data successfully saved!")
else
print("There was an error when saving data")
warn(errormessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, errormessage = pcall(function()
ClickDataStore:SetAsync(player.UserId.."Clicks",player.leaderstats.Clicks.Value)
end)
if success then
print("Player Data successfully saved!")
else
print("There was an error when saving data")
warn(errormessage)
end
end)```
Easy. Just use a while wait() do loop. add the following code to the bottom of your player added event.
while wait(60) do
local success, err = pcall(function()
ClickDataStore:SetAsync(player.UserId.."Clicks", Clicks.Value)
end)
if success then
print("Data auto saved")
else
warn(err)
end
end
Read this article. It tells you all of the limitations of datastores, common errors, and other stuff. This should answer all of your questions. Data Stores
So is this what the final script should look like?
local ClickDataStore = DataStoreService:GetDataStore("ClickDataStore")
game.Players.PlayerAdded:Connect(function(player)
local stats = Instance.new("Folder")
stats.Name = "leaderstats"
stats.Parent = player
local Clicks = Instance.new("IntValue")
Clicks.Name = "Clicks"
Clicks.Parent = player.leaderstats
local data
local success, errormessage = pcall(function()
data = ClickDataStore:GetAsync(player.userId.."Clicks")
while wait(60) do
local success, err = pcall(function()
ClickDataStore:SetAsync(player.UserId.."Clicks", Clicks.Value)
end)
if success then
print("Data auto saved")
else
print("There was an error when saving data")
warn(error)
end
end
end)
if success then
Clicks.Value = data
print("Player Data successfully saved!")
else
print("There was an error when saving data")
warn(errormessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, errormessage = pcall(function()
ClickDataStore:SetAsync(player.UserId.."Clicks",player.leaderstats.Clicks.Value)
end)
if success then
print("Player Data successfully saved!")
else
print("There was an error when saving data")
warn(errormessage)
end
end)
No, put it at the bottom. Here, I fixed it for you.
game.Players.PlayerAdded:Connect(function(player)
local stats = Instance.new("Folder")
stats.Name = "leaderstats"
stats.Parent = player
local Clicks = Instance.new("IntValue")
Clicks.Name = "Clicks"
Clicks.Parent = stats
local data
local success, errormessage = pcall(function()
data = ClickDataStore:GetAsync(player.userId.."Clicks")
end)
if success then
Clicks.Value = data
print("Player Data successfully saved!")
else
print("There was an error when saving data")
warn(errormessage)
end
while wait(60) do
local success, err = pcall(function()
ClickDataStore:SetAsync(player.UserId.."Clicks", Clicks.Value)
end)
if success then
print("Data auto saved")
else
print("There was an error when saving data")
warn(error)
end
end
end)
local ClickDataStore = DataStoreService:GetDataStore("ClickDataStore")
game.Players.PlayerAdded:Connect(function(player)
local stats = Instance.new("Folder")
stats.Name = "leaderstats"
stats.Parent = player
local Clicks = Instance.new("IntValue")
Clicks.Name = "Clicks"
Clicks.Parent = stats
local data
local success, errormessage = pcall(function()
data = ClickDataStore:GetAsync(player.userId.."Clicks")
end)
if success then
Clicks.Value = data
print("Player Data successfully saved!")
else
print("There was an error when saving data")
warn(errormessage)
end
while wait(60) do
local success, err = pcall(function()
ClickDataStore:SetAsync(player.UserId.."Clicks", Clicks.Value)
end)
if success then
print("Data auto saved")
else
print("There was an error when saving data")
warn(error)
end
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, errormessage = pcall(function()
ClickDataStore:SetAsync(player.UserId.."Clicks",player.leaderstats.Clicks.Value)
end)
if success then
print("Player Data successfully saved!")
else
print("There was an error when saving data")
warn(errormessage)
end
end)
local ClickDataStore = DataStoreService:GetDataStore("ClickDataStore")
game.Players.PlayerAdded:Connect(function(player)
local stats = Instance.new("Folder")
stats.Name = "leaderstats"
stats.Parent = player
local Clicks = Instance.new("IntValue")
Clicks.Name = "Clicks"
Clicks.Parent = stats
local data
local success, errormessage = pcall(function()
data = ClickDataStore:GetAsync(player.userId.."Clicks")
end)
if success then
Clicks.Value = data
print("Player Data successfully saved!")
else
print("There was an error when saving data")
warn(errormessage)
end
while wait(60) do
local success, error = pcall(function()
ClickDataStore:SetAsync(player.UserId.."Clicks", Clicks.Value)
end)
if success then
print("Data auto saved")
else
print("There was an error when saving data")
warn(error)
end
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, errormessage = pcall(function()
ClickDataStore:SetAsync(player.UserId.."Clicks",player.leaderstats.Clicks.Value)
end)
if success then
print("Player Data successfully saved!")
else
print("There was an error when saving data")
warn(errormessage)
end
end)