I have 3 values i want to store in the datastore, it was working fine before when i only had the seconds and minutes but after i added purchases it suddenly stopped working. ive been trying to fix it for hours and im honestly confused because i dont get any errors at all.
i added the pcalls with the print messages and it seems like everything it working fine because it gets through but when i open the game again it hasnt updated from the old data.
I appreciate all the help thanks in advance!
local DataStoreService = game:GetService("DataStoreService")
local MyDataStore = DataStoreService:GetDataStore("MyDataStore")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
-- group ranks
local Rank = Instance.new("StringValue")
Rank.Name = "Rank"
Rank.Parent = leaderstats
Rank.Value = player:GetRoleInGroup(11392499)
local Minutes = Instance.new("IntValue")
Minutes.Name = "Minutes"
Minutes.Parent = leaderstats
local Seconds = Instance.new("IntValue")
Seconds.Name = "Seconds"
Seconds.Parent = leaderstats
local Purchases = Instance.new("IntValue")
Purchases.Name = "Purchases"
Purchases.Parent = leaderstats
local isLoaded = Instance.new("BoolValue")
isLoaded.Name = "isLoaded"
isLoaded.Value = false
isLoaded.Parent = player
local DataTable
-- getting saved data
local success, errormessage = pcall(function()
DataTable = MyDataStore:GetAsync(player.UserId)
end)
if success then
print("success!")
if DataTable then
Minutes.Value = DataTable["Minutes"]
Seconds.Value = DataTable["Seconds"]
Purchases.Value = DataTable["Purchases"]
isLoaded.Value = true
print("Data loaded successfully!")
end
else
warn("Could not save data! Error code: "..errormessage)
end
-- updating data every minute
while wait (60) do
local success, errormessage = pcall(function()
MyDataStore:UpdateAsync(player.UserId, function(OldValue)
local saveTable = OldValue or {}
saveTable["Minutes"] = player.leaderstats.Minutes.Value;
saveTable["Seconds"] = player.leaderstats.Seconds.Value;
saveTable["Purchases"] = player.leaderstats.Purchases.Value;
return saveTable
end)
end)
if success then
print("autosave completed!")
else
warn("Could not save data! Error code: "..errormessage)
end
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, errormessage = pcall(function()
MyDataStore:UpdateAsync(player.UserId, function(OldValue)
local saveTable = OldValue or {}
saveTable["Minutes"] = player.leaderstats.Minutes.Value;
saveTable["Seconds"] = player.leaderstats.Seconds.Value;
saveTable["Purchases"] = player.leaderstats.Purchases.Value;
return saveTable
end)
end)
end)