hey guys!
I am using some kind of “player handler”
But, i’m having a trouble, data isn’t saving
This is the module;
local tmp = require(script.profile)
local module = {}
local DataHandler = require(script.Parent.Datastore)
module.__index = module
function module.getplayer(plr)
return module[plr] or module.new(plr)
end
function module.new(plr)
assert(plr ~= nil,"plr's nil.")
local self = setmetatable(tmp,module)
module[plr] = self
self.Player = plr
return self
end
-- // DATA
function module:UpdateData(Information)
self.ToSave = Information
end
function module:SaveData()
print("Saving data.")
DataHandler.SaveData(self.Player.UserId,self.ToSave)
end
function module:GetData()
local data = DataHandler.GetData(self.Player.UserId)
if (data) then
return data
end
print(data)
return nil
end
Data keeps printing nil.
This is data template or “tmp” in code;
return {
ToSave = {
Races = {
["Winned Races"] = 0;
};
Rank = 0;
Cash = 0;
XP = 0;
SelectedCar = 'V5Buggy';
Vehicles = {
["V5Buggy"] = {
VehicleTexture = '';
};
};
};
NonSaving = {
InRace = false;
}
}
This is where I call the Save
Players.PlayerAdded:Connect(function(Player)
-- // Retreiving Data
local PlayerStorage = PlayersHandler.getplayer(Player)
local Data = PlayerStorage:GetData()
-- // Player leaving/ data saving.
local connection
connection = Players.PlayerRemoving:Connect(function(P)
if (Player == P) then
connection:Disconnect()
end
PlayerStorage:SaveData()
end)
end)
For those interesed, this is the Data Module;
Data module
local DSS = game:GetService("DataStoreService")
local Datastore = DSS:GetDataStore("DATA - PlayerStorage")
local data = {}
function data.SaveData(Key,ToSave)
local Attempts = 0
repeat
local suc,er = pcall(function()
Datastore:UpdateAsync(Key,function(Old)
return ToSave or Old
end)
end)
if (suc) then
print("Successfully saved data.")
break
else
warn(er,suc)
end
Attempts += 1
wait(1)
until not suc or Attempts >= 3
end
function data.GetData(key)
local suc,result = pcall(Datastore.GetAsync,Datastore,key)
if (suc) then
if (result) then
return result
end
else
warn(suc)
return data.GetData(key)
end
return nil
end
return data