local ds = game:GetService("DataStoreService"):GetDataStore("PVZTR")
warn ("It does exist")
local function SaveData(plr)
local Data = {}
for _, Values in pairs (plr.Values:GetChildren()) do
table.insert(Data, Values)
ds:UpdateAsync(plr.UserId, Data)
end
end
local function LoadData(plr)
ds:GetAsync(plr.UserId)
end
--loading data
local player
local CanSave = false
game.Players.PlayerAdded:Connect(function(plr)
player = plr
local folder = Instance.new("Folder", plr)
folder.Name = "Values"
local stage = Instance.new("IntValue", folder)
stage.Name = "Stage"
local level = Instance.new("IntValue", folder)
level.Name = "Level"
local success, err = pcall(function()
LoadData(plr)
end)
if not success then
local tries = 10
while task.wait() do
if tries ~= 0 then
warn("Loading failed, retrying. Tries until rejoin:", tries)
tries -= 1
LoadData(plr)
elseif tries == 0 then
warn("rejoining.")
game:GetService("TeleportService"):Teleport(game.PlaceId, plr)
elseif success then
print("Loaded")
CanSave = true
end
end
end
end)
--saving data
game:BindToClose(function()
warn("Trying to save")
if CanSave then
print("Data is not corrupted.")
local success, err = pcall(function()
SaveData(player)
end)
if not success then
warn("Didn't save, retrying.")
repeat SaveData(player) until success
elseif success then
print("Saved!")
end
end
end)
there has to be an issue /w the if statements but i can’t figure it out i am too dumb. it’s singleplayer btw
I don’t understand why you need to repeat the save everywhere if it fails, as it won’t really help and isn’t necessary if you make your game right (in terms of database and scripts)
local ds = game:GetService("DataStoreService"):GetDataStore("PVZTR")
local function SaveData(plr)
local Data = {}
for _, Values in pairs (plr.Values:GetChildren()) do
table.insert(Data, Values)
end
ds:UpdateAsync(plr.UserId, Data)
end
local function LoadData(plr)
return ds:GetAsync(plr.UserId)
end
game.Players.PlayerAdded:Connect(function(plr)
local folder = Instance.new("Folder", plr)
folder.Name = "Values"
local stage = Instance.new("IntValue", folder)
stage.Name = "Stage"
local level = Instance.new("IntValue", folder)
level.Name = "Level"
local success, data = pcall(function()
LoadData(plr)
end)
if not success then
local tries = 0
while task.wait(math.pow(2, tries)) do
if tries>=3 then
warn("rejoining.")
game:GetService("TeleportService"):Teleport(game.PlaceId, plr)
break
else
tries+=1
success, data = pcall(function()
LoadData(plr)
end)
if success then
print("Loaded")
break
end
end
end
end
--set data for player (stage.Value=data.stage - EXAMPLE!!!)
end)
game.Players.PlayerRemoving:Connect(function(plr)
SaveData(plr)
end)
--saving data
game:BindToClose(function()
for _, v in pairs(game.Players:GetPlayers()) do
SaveData(v)
end
end)
I wouldn’t say what could be the reason for not saving? You also need to remember about the restrictions on Update / Set Async, and while the code is being executed, the player will simply leave the game.
i never said that was the issue, but more as an issue if it fails to save. better be safe than sorry. also loading isn’t bad as long as you have measures if it fails, like not saving but you should pcall saving since that is really important.
thanks, but now "Expected ) to close ( at column 16, got ‘function’ which was copy and pasted from the snippet you gave.
local function SaveData(plr)
local Data = {}
for _, Values in pairs (plr.Values:GetChildren()) do
table.insert(Data, Values)
end
ds:UpdateAsync(plr.UserId function(pastData)
return Data
end
end
You missed ). And I’m not sure if the script will work since you are saving the Instance to a table. Use it:
local function SaveData(plr)
local Data = {}
for _, Values in pairs (plr.Values:GetChildren()) do
Data[Values.Name] = Values.Value
end
ds:UpdateAsync(plr.UserId function(pastData)
return Data
end)
end
small question, haven’t tested it yet: WHERE ARE THE VALUES ADDED TO THE TABLE?!
my brain is gonna implode
same error btw
local function SaveData(plr)
local Data = {}
for _, Values in pairs (plr.Values:GetChildren()) do
Data[Values.Name] = Values.Value
end
ds:UpdateAsync(plr.UserId function(pastData)
return Data
end)
end
local function SaveData(plr)
local Data = {}
for _, Values in pairs (plr.Values:GetChildren()) do
Data[Values.Name] = Values.Value
end
print(Data)
ds:UpdateAsync(plr.UserId, function(pastData)
return Data
end)
end
and
replace --set data for player (stage.Value=data.stage - EXAMPLE!!!) to print(data)