Hello there,
The past couple of days, I have been having trouble getting player data and saving it. I have tried nearly everything I can do because I am trying to get old data and putting it into one whole table and it doesn’t seem to be working and I have no idea why?
Here is the module code:
local module = {}
local modulefolder = game:GetService("ReplicatedStorage").SharedModules.Services
local serviceModule = require(modulefolder.GettingServices)
local moduleLoader = require(game:GetService("ReplicatedStorage").Bolo.ModuleLoader.Loader).new()
local DataStore2 = require(game:GetService("ServerScriptService"):WaitForChild("DataStore2"))
local DefaultPlayerDataModule = require(game.ReplicatedStorage.SharedModules.Data.DefaultPlayerDataModule)
local truckKeys = moduleLoader:get("TruckNames")
local defaultTable = DefaultPlayerDataModule:GetDefaultData()
local Data = {}
Data.__index = Data
function Data:new(player)
local self = setmetatable({}, Data)
self.plr = player
self.dbToSave = DataStore2("PlayerData", player)
self.TrucksModule = {}
self.DefaultPlayerDataModule = DefaultPlayerDataModule
self._OldData = {
PlayerTrucks = DataStore2("PlayerTrucks", player):Get({}),
plrSettings = DataStore2("PlayerSettings", player):Get({}),
Mods = DataStore2("Mods", player):Get({}),
Money = DataStore2("DIBank", player):Get(0),
Deliveries = DataStore2("DeliverySave", player):Get(0),
Banning = DataStore2("Banlist", player):Get(0),
newPlayer = DataStore2("IsNewPlayer", player):Get(true),
oldDataUsed = DataStore2("OldDataUsed", player):Get(false),
}
self.playerData = self.dbToSave:Get(defaultTable)
self.MAX_RETRIES = 3
return self
end
function Data:migratingOldtoNew()
if not self._OldData.oldDataUsed then
if not next(self.playerData) then -- Check if playerData is empty or has default values
self.playerData = {
trucks = self._OldData.PlayerTrucks,
mods = self._OldData.Mods,
settings = self._OldData.plrSettings,
money = self._OldData.Money,
deliveries = self._OldData.Deliveries,
isBanned = self._OldData.Banning,
isNew = self._OldData.newPlayer,
Fuel = 100,
}
self.dbToSave:Set(self.playerData)
DataStore2("OldDataUsed", self.plr):Set(true)
end
end
end
function Data:setupPlayerSettings(player)
local settingsFolder = Instance.new("Folder", player)
settingsFolder.Name = "PlrSettings"
local playerData = self.dbToSave:Get(self.DefaultPlayerDataModule:GetDefaultData())
local settings = {"DOF", "Shadows", "Trees", "Sunrays"}
for _, setting in ipairs(settings) do
local boolValue = Instance.new("BoolValue", settingsFolder)
boolValue.Name = setting
boolValue.Value = playerData.settings[setting] ~= nil and playerData.settings[setting] or false
end
end
function Data:loading(player)
local playerData = self.dbToSave:Get(self.DefaultPlayerDataModule:GetDefaultData())
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"
self:setupPlayerSettings(player)
local money = Instance.new("IntValue", leaderstats)
money.Name = "Money"
money.Value = playerData.money
local deliveries = Instance.new("IntValue", leaderstats)
deliveries.Name = "Deliveries"
deliveries.Value = playerData.deliveries
local ban = Instance.new("NumberValue", player)
ban.Name = "IsBanned"
ban.Value = playerData.isBanned
local function updateStats(updatedValue)
local updatedData = self.dbToSave:Get(updatedValue)
money.Value = updatedData.money
deliveries.Value = updatedData.deliveries
end
local success, message = pcall(function()
self.dbToSave:OnUpdate(updateStats)
end)
if not success then
warn(message)
end
end
function Data:createTrucksFolder(player)
local playerTrucks = player:FindFirstChild("OwnedTrucks")
if not playerTrucks then
playerTrucks = Instance.new("Folder")
playerTrucks.Name = "OwnedTrucks"
playerTrucks.Parent = player
end
local vehiclesLoaded = player:FindFirstChild("VehiclesLoaded")
if vehiclesLoaded then
self:createOwnedVehicles(player, playerTrucks)
vehiclesLoaded.Value = true
else
warn(string.format("Failed to find 'VehiclesLoaded' for player: %s", player.Name))
end
end
function Data:createOwnedVehicles(player, plrTrucks)
local dbToSave = self.dbToSave
local playerData = dbToSave:Get(DefaultPlayerDataModule:GetDefaultData())
for _, v in pairs(truckKeys:GetAllKeys()) do
local truck = Instance.new("BoolValue", plrTrucks)
truck.Name = v
end
if playerData.trucks then
for i, v in pairs(playerData.trucks) do
local val = plrTrucks:FindFirstChild(i)
if val then
val.Value = v
end
end
end
end
function Data:saveDataTables(firstset)
local saveprocess = {}
for _, v in ipairs(firstset:GetChildren()) do
saveprocess[v.Name] = v.Value
end
return saveprocess
end
function Data:save()
local player = self.plr
local playerTrucks = player:FindFirstChild("OwnedTrucks")
local playerSettings = player:FindFirstChild("PlrSettings")
local playerMods = player:FindFirstChild("PlayerData")
local leaderstats = player:FindFirstChild("leaderstats")
if not leaderstats then
warn("Leaderstats not found for player:", player.Name)
return
end
local money = leaderstats:FindFirstChild("Money")
local deliveries = leaderstats:FindFirstChild("Deliveries")
local isBanned = player:FindFirstChild("IsBanned")
local isNew = player:FindFirstChild("NewToGame")
if not money or not deliveries or not isBanned or not isNew then
warn("Necessary player data not found for player:", player.Name)
return
end
local dataGettingSaved = {
trucks = self:saveDataTables(playerTrucks),
mods = self:saveDataTables(playerMods),
settings = self:saveDataTables(playerSettings),
money = money.Value,
deliveries = deliveries.Value,
isBanned = isBanned.Value,
isNew = isNew.Value,
Fuel = 100,
}
for attempt = 1, self.MAX_RETRIES do
local success, errorMessage = pcall(function()
self.dbToSave:Set(dataGettingSaved)
end)
if success then
print(string.format("Successfully saved data for player: %s", player.Name))
break
else
warn(string.format("Failed to save data for player: %s on attempt %d | Error: %s", player.Name, attempt, errorMessage))
wait(2)
end
end
end
return Data
If anyone could point out where I have gone wrong with it, that would be amazing!
- Sim