Im unsure why this keep happening but sometimes here and there i lose data in my game
i dont know what the issue is and im unsure if its just roblox or my code is bad
local DSS = game:GetService("DataStoreService")
local ss = game:GetService("ServerStorage")
local sss = game:GetService("ServerScriptService")
local DataStore = DSS:GetDataStore("DataV1")
local playersleft = 0
game.Players.PlayerAdded:Connect(function(plr)
playersleft = playersleft + 1
local datafolder = Instance.new("Folder",plr)
datafolder.Name = "data"
local NumberValues ={
"Spins",
"Luck",
"Gems",
}
local StringValues = {
"Aura",
"Trait",
}
for i,v in pairs(NumberValues) do
local A = Instance.new("NumberValue", datafolder)
A.Name = v
end
for i,v in pairs(StringValues) do
local A = Instance.new("StringValue", datafolder)
A.Name = v
end
local PlayerUserId = "Player_"..plr.UserId
local Data
local success, err = pcall(function()
Data = DataStore:GetAsync(PlayerUserId)
end)
if success then
if Data then
plr:FindFirstChild("data").Trait.Value = Data.traitsave
plr:FindFirstChild("data").Aura.Value = Data.aurasave
plr:FindFirstChild("data").Spins.Value = Data.spinssave
plr:FindFirstChild("data").Luck.Value = Data.lucksave
plr:FindFirstChild("data").Gems.Value = Data.gemssave
else
plr:FindFirstChild("data").Trait.Value = "Traitlesss"
plr:FindFirstChild("data").Aura.Value = "Auraless"
plr:FindFirstChild("data").Spins.Value = 100
plr:FindFirstChild("data").Luck.Value = 1
plr:FindFirstChild("data").Gems.Value = 0
end
else
print("why")
end
end)
local Bindable = Instance.new("BindableEvent")
game.Players.PlayerRemoving:Connect(function(plr)
local PlayerUserId = "Player_"..plr.UserId
local Data = {
traitsave = plr:FindFirstChild("data").Trait.Value;
aurasave = plr:FindFirstChild("data").Aura.Value;
spinssave = plr:FindFirstChild("data").Spins.Value;
lucksave = plr:FindFirstChild("data").Luck.Value;
gemssave = plr:FindFirstChild("data").Gems.Value;
}
local success, err = pcall(function()
DataStore:SetAsync(PlayerUserId, Data)
playersleft = playersleft - 1
Bindable:Fire()
end)
if success then
print("Saved Data")
else
print("Off Error")
warn(err)
end
end)
game:BindToClose(function()
while playersleft > 0 do
Bindable.Event:Wait()
end
end)
the game loads data fine but sometimes rarely i get an error when a player is leaving the game