Lately my games server's have been crashing randomly and players are complaining of data loss! Other Devs have suggested adding a save button but I have no idea how to script that, seeing as i'm terrible with Datastores. Is there any way to save in the event of a crash ? Also is my Datastore save and load setup correctly and efficiently?
local saveAs = "LS"
local dataStore = game:GetService("DataStoreService"):GetDataStore(saveAs)
local Players = game:GetService("Players")
Players.PlayerAdded:connect(function(player)
local stats = player:WaitForChild("leaderstats")
local data = dataStore:GetAsync(player.UserId)
if data then
for name,value in next, data do
stats[name].Value = value
end
end
end)
while wait(60) do
for _, player in pairs(game.Players:GetChildren()) do
local stats = player:FindFirstChild("leaderstats")
if stats then
local data = {}
for _,stat in ipairs(stats:GetChildren()) do
data[stat.Name] = stat.Value
end
dataStore:SetAsync(player.UserId,data)
end
end
end
Players.PlayerRemoving:connect(function(player)
local stats = player:FindFirstChild("leaderstats")
if stats then
local data = {}
for _,stat in ipairs(stats:GetChildren()) do
data[stat.Name] = stat.Value
end
dataStore:SetAsync(player.UserId,data)
end
end)

2 comments
Move the PlayerRemoving event to before the while loop. You while loop does not terminate, therefore your event is never reached nor heard.
Alright will do is there anything else I can fix or take a look at?