So I’m making a datastore and I’m trying to make it good but I’m a starter scripter and I don’t know too much about those fancy models. what do you think I can do to prevent data loss?
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("MoneyStats")
Players.PlayerAdded:Connect(function(Player)
if Player then
local Folder = Instance.new("Folder")
Folder.Name = "leaderstats"
Folder.Parent = Player
local Bank = Instance.new("IntValue")
Bank.Name = "Bank"
Bank.Parent = Folder
Bank.Value = 100
local Cash = Instance.new("IntValue")
Cash.Name = "Cash"
Cash.Parent = Folder
Cash.Value = 50
local Data = DataStore:GetAsync(Player.UserId)
if Data then
Bank.Value = Data.Bank
Cash.Value = Data.Cash
end
while wait(300) do
game.ReplicatedStorage.Remotes.Message.MessageEdit:FireAllClients("notification","DATA SAVED SUCCESSFULLY",Color3.new(0.0352941, 0.580392, 0))
DataStore:SetAsync(Player.UserId, {
["Bank"] = Bank.Value;
["Cash"] = Cash.Value;
})
end
end
end)
Players.PlayerRemoving:Connect(function(Player)
DataStore:SetAsync(Player.UserId, {
["Bank"] = Player.leaderstats.Bank.Value;
["Cash"] = Player.leaderstats.Cash.Value;
})
end)