Hello,
I am currently using the script below to save a player’s leaderstats, which are Credits, Playtime, and Prizes Won. The script also checks to see if a player has data from a previous datastore, and transfers it over to the newer datastore if they do.
If a player doesn’t have old or new data, their leaderstats are defaulted to 300, 0, 0, and the data saves correctly. However, if the player has old data (Ex: 445, 3, 2), the script fails to transfer the data to the newer datastore and instead, their values are set to 0, 0, 0. What might be causing this?
Thank You!
local DataStoreService = game:GetService("DataStoreService")
local newDataStore = DataStoreService:GetDataStore("LeaderstatsData") -- New DataStore
local oldDataStoreA = DataStoreService:GetDataStore("LeaderstatsData") -- Old Credits
local oldDataStoreB = DataStoreService:GetDataStore("LeaderstatsData2") -- Old Playtime
local oldDataStoreC = DataStoreService:GetDataStore("LeaderstatsData3") -- Old Prizes Won
-- Default player data
local function getDefaultData()
return {
Credits = 300,
Playtime = 0,
PrizesWon = 0
}
end
-- Load player data
local function loadPlayerData(player)
local success, playerData = pcall(function()
return newDataStore:GetAsync("DataKey_" .. player.UserId)
end)
if success and playerData then
return playerData -- Data found in new DataStore
else
if not success then
warn("Failed to load data for player", player.UserId)
end
-- Attempt to load from old DataStores
local oldCredits, oldPlaytime, oldPrizesWon
local successA = pcall(function()
oldCredits = oldDataStoreA:GetAsync(player.UserId)
end)
local successB = pcall(function()
oldPlaytime = oldDataStoreB:GetAsync(player.UserId)
end)
local successC = pcall(function()
oldPrizesWon = oldDataStoreC:GetAsync(player.UserId)
end)
-- Check if any old data exists and migrate it
if successA or successB or successC then
warn("Migrating old data for player", player.UserId)
local migratedData = {
Credits = oldCredits or 300,
Playtime = oldPlaytime or 0,
PrizesWon = oldPrizesWon or 0
}
-- Save migrated data to the new DataStore
pcall(function()
newDataStore:SetAsync("DataKey_" .. player.UserId, migratedData)
end)
return migratedData
else
-- No data found in old DataStores, apply defaults
return getDefaultData()
end
end
end
-- Save player data
local function savePlayerData(player)
local playerData = {
Credits = player.leaderstats.Credits.Value,
Playtime = player.leaderstats.Playtime.Value,
PrizesWon = player.leaderstats["Prizes Won"].Value
}
local success, errorMessage = pcall(function()
newDataStore:SetAsync("DataKey_" .. player.UserId, playerData)
end)
if not success then
warn("Failed to save data for player", player.UserId, errorMessage)
end
end
-- Player added event
game.Players.PlayerAdded:Connect(function(player)
-- Create leaderstats folder
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
-- Create leaderstats values
local credits = Instance.new("IntValue", leaderstats)
credits.Name = "Credits"
local playtime = Instance.new("IntValue", leaderstats)
playtime.Name = "Playtime"
local prizesWon = Instance.new("IntValue", leaderstats)
prizesWon.Name = "Prizes Won"
-- Load and apply player data
local playerData = loadPlayerData(player)
credits.Value = playerData.Credits
playtime.Value = playerData.Playtime
prizesWon.Value = playerData.PrizesWon
end)
-- Player removing event
game.Players.PlayerRemoving:Connect(savePlayerData)
-- Periodic autosave
task.spawn(function()
while task.wait(60) do
for _, player in pairs(game.Players:GetPlayers()) do
savePlayerData(player)
end
end
end)