Hi, so I recently have made an autosave script, but my game also has a save button. Is there any way I can make autosaving an option? Thanks.
Autosave script:
local DataStoreService = game:GetService("DataStoreService")
local CashStore = DataStoreService:GetDataStore("Cash")
game.Players.PlayerAdded:Connect(function(Player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = Player
local Cash = Instance.new("IntValue")
Cash.Name = "Cash"
Cash.Value = 0
Cash.Parent = leaderstats
local data
local Success, Error = pcall(function()
data = CashStore:GetAsync(Player.UserId.."-cash")
end)
if Success then
Cash.Value = data
print("Data Found!")
else
warn(Error)
end
end)
local queue = 0
game.Players.PlayerRemoving:Connect(function(Player)
queue += 1
local Sucess, Error = pcall(function()
CashStore:SetAsync(Player.UserId.."-cash", Player.leaderstats.Cash.Value)
end)
if Sucess then
print("Data Saved!")
queue -= 1
else
warn(Error)
end
end)
game:BindToClose(function()
repeat wait() until queue == 0
end)
Save button script:
local ds = game:GetService("DataStoreService")
local rc = ds:GetDataStore("RebirthsSave")
local lc = ds:GetDataStore("Levels")
local ss = ds:GetDataStore("SpeedValues")
local qc = ds:GetDataStore("PlayerQuests")
local gs = ds:GetDataStore("GemsSave")
local ReplicatedStorage = game.ReplicatedStorage
local function create_table(player)
local player_stats = {}
for _, stat in pairs(player.Quests:GetChildren()) do
player_stats[stat.Name] = stat.Value
end
return player_stats
end
ReplicatedStorage.Save.OnServerEvent:Connect(function(player)
local success, errormessage = pcall(function()
rc:SetAsync(player.UserId, player.leaderstats.Rebirths.Value)
lc:SetAsync(player.UserId, player.leaderstats.Levels.Value)
ss:SetAsync(player.UserId, player.Data.attackspeed.Value)
gs:SetAsync(player.UserId, player.leaderstats.Gems.Value)
local plrstats = create_table(player)
qc:SetAsync(player.UserId, plrstats)
end)
if success then
print("Success For Saving")
else
warn(errormessage)
end
end)