Hey Dev Forum
For a while now I have noticed that whenever a player’s data is unable to save it usually just resets. Is their a way I can make sure it doesn’t reset?
Function to Save Data
local function Save(Plr)
print("Attempted to saved player please wait for data response")
local PlrStats = CreateTable(Plr)
local Sucess, Error = pcall(function()
local PlrUserID = "Player_"..Plr.UserId
PlayerData:SetAsync(PlrUserID, PlrStats)
end)
if not Sucess then
warn("Not Able To Save")
else
print("Sucessfully Saved "..Plr.Name)
end
end
Entire Data Script
local DSS = game:GetService("DataStoreService")
local PlayerData = DSS:GetDataStore("RaceData001")
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local function CreateTable(Plr)
local PlrStats = {}
for _,V in ipairs(Plr.RData:GetChildren()) do
PlrStats[V.Name] = V.Value
end
return PlrStats
end
local function Save(Plr)
print("Attempted to saved player please wait for data response")
local PlrStats = CreateTable(Plr)
local Sucess, Error = pcall(function()
local PlrUserID = "Player_"..Plr.UserId
PlayerData:SetAsync(PlrUserID, PlrStats)
end)
if not Sucess then
warn("Not Able To Save")
else
print("Sucessfully Saved "..Plr.Name)
end
end
Players.PlayerAdded:Connect(function(Plr)
local RData = Instance.new("Folder", Plr)
RData.Name = "RData"
local RaceRolls = Instance.new("IntValue")
RaceRolls.Parent = RData
RaceRolls.Name = "RaceRolls"
RaceRolls.Value = 5
local Race = Instance.new("StringValue")
Race.Parent = RData
Race.Name = "RaceData"
Race.Value = "Human"
local PlrUserId = "Player_"..Plr.UserId
local StoreData
local Sucess, Error = pcall(function()
StoreData = PlayerData:GetAsync(PlrUserId)
end)
if not Sucess then
warn("Not Able To Save")
end
if type(StoreData) == "table" then
for I,V in pairs(StoreData) do
if RData:FindFirstChild(I) then
RData:FindFirstChild(I).Value = V
end
end
end
end)
Players.PlayerRemoving:Connect(function(Plr)
Save(Plr)
end)
game:BindToClose(function()
for _, Plr in ipairs(Players:GetPlayers()) do
Save(Plr)
end
end)