So, I want to make a datastore, but it only saves sometimes. I want it to save all the time. Here is the code
local Data = game:GetService("DataStoreService")
local DataStore1 = Data:GetDataStore("DataStore1")
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.Parent = leaderstats
local wins = Instance.new("IntValue")
wins.Name = "Wins"
wins.Parent = leaderstats
local playerUserID = "Player_"..player.UserId
local data2
local success, errormessage = pcall(function()
data2 = DataStore1:GetAsync(playerUserID)
end)
if success then
cash.Value = data2.Cash
wins.Value = data2.Wins
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local playerUserID = "Player_"..player.UserId
local data3 = {
Cash = player.leaderstats.Cash.Value;
Wins = player.leaderstats.Wins.Value
}
local success, errormessage = pcall(function()
DataStore1:SetAsync(playerUserID, data3)
end)
if success then
print("Data has saved")
else
print("Error for saved message")
warn(errormessage)
end
end)