Hello fine developers of the DevForums,
I’ve recently gotten into Object-Oriented Programming (OOP) and it’s been very interesting and fun. I learn from @jonbyte’s highly detailed and cool tutorial (thanks JonByte, you’re cool). You get to program in another way where you could make your own methods as well, which is even more fun. I then realized that you could program DataStores in OOP, so I made an attempt.
local default_data = {};
local PlayerData = {};
PlayerData._index = PlayerData;
function PlayerData.new(Player)
local self = setmetatable({}, PlayerData);
self.CashData = game:GetService("DataStoreService"):GetDataStore("Cash");
self.DataKey = Player.UserId .. "-cash";
self.Value = Player.leaderstats.Cash.Value;
return self;
end
function PlayerData:Save()
local Result;
local Success, errmessage = pcall(function()
Result = self.CashData:SetAsync(self.DataKey, self.Value);
end)
if Success then
print("Data save was successful.");
else
print("There was a problem with saving the data.");
warn(errmessage);
end
end
function PlayerData:Load()
local Result;
local success, errmessage = pcall(function()
Result = self.CashData:GetAsync(self.DataKey);
end)
if success then
if Result then
print("Current Cash: " .. Result);
else
return nil;
end
else
print("There was a problem with loading the data.");
warn(errmessage);
end
end
This was my attempt, but then I ran into a question that I couldn’t answer…
How would I load the player’s data? How would I check if the player already had data before loading it? There were a few problems here and I think it’s because of the way I did the whole thing. I don’t fully understand all the aspects of OOP yet. So I may be doing something wrong here. I don’t really know what to do so what should I do?