What do you want to achieve? I want a datastore that will sucessfully save players data and minimise data loss
local DataStoreService = game:GetService('DataStoreService')
local DataStore = DataStoreService:GetDataStore("Test2")
local HttpService = game:GetService('HttpService')
local Players = game:GetService('Players')
Players.PlayerAdded:Connect(function(plr)
local Folder = game.ReplicatedStorage.Data:Clone()
Folder.Parent = plr
local data = DataStore:GetAsync(plr.UserId)
if data then
print ("Player Has Data")
plr.Data.HasData.Value = true
for i,v in pairs(plr.Data:GetChildren()) do
v.Value = data[v.Name]
end
else
plr.Data.HasData.Value = false
print("Player Data Not Found")
end
end)
Players.PlayerRemoving:Connect(function(plr)
local Folder = plr.Data
local data = {}
for i,v in pairs(Folder:GetChildren()) do
data[v.Name] = v.Value
end
DataStore:SetAsync(plr.UserId, data)
end)
function saveAllData()
local data = {}
for _,v in pairs(game.Players:GetChildren()) do
for i,stat in ipairs(v.Data:GetChildren()) do
data[stat.Name] = stat.Value
end
if data ~= nil then
pcall(function()
DataStore:SetAsync(v.UserId, data)
end)
end
wait(.05)
end
allDataSaved = true
end
game:BindToClose(function()
saveAllData()
repeat wait(1) until allDataSaved == true
end)
If you want that, then you can use ProfileService, a custom module that does almost everything for you and is good at preventing data loss. Use the instructions in the post, I’m not very good at explaining this.
If you have any questions or concerns then you can ask me
local DataStoreService = game:GetService('DataStoreService')
local DataStore = DataStoreService:GetDataStore("Test2")
local HttpService = game:GetService('HttpService')
local Players = game:GetService('Players')
-- Handle player joining
Players.PlayerAdded:Connect(function(plr)
local Folder = game.ReplicatedStorage.Data:Clone()
Folder.Parent = plr
local success, data = pcall(function()
return DataStore:GetAsync(tostring(plr.UserId))
end)
if success and data then
print("Player Has Data")
plr.Data.HasData.Value = true
for _, v in pairs(plr.Data:GetChildren()) do
local savedValue = data[v.Name]
if savedValue ~= nil then
v.Value = savedValue
end
end
else
plr.Data.HasData.Value = false
print("Player Data Not Found")
end
end)
-- Handle player leaving
Players.PlayerRemoving:Connect(function(plr)
local Folder = plr.Data
local data = {}
for _, v in pairs(Folder:GetChildren()) do
data[v.Name] = v.Value
end
local success, _ = pcall(function()
DataStore:SetAsync(tostring(plr.UserId), data)
end)
if not success then
print("Failed to save player data for " .. plr.Name)
end
end)
-- Save data for all players when the game closes
function saveAllData()
for _, v in pairs(game.Players:GetPlayers()) do
local Folder = v.Data
local data = {}
for _, stat in ipairs(Folder:GetChildren()) do
data[stat.Name] = stat.Value
end
local success, _ = pcall(function()
DataStore:SetAsync(tostring(v.UserId), data)
end)
if not success then
print("Failed to save player data for " .. v.Name)
end
end
end
game:BindToClose(function()
saveAllData()
end)
i made some improvements and added comments to your data script, if you want you can test it