So I’m making a DataStore Module for me to use, but I was wondering how to Organize it, or if I already have a good enough Organization.
This is mainly a test of what I can do with Scripts, and how I could Manipulate them.
--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------
local DataStoreService = game:GetService("DataStoreService")
local RunService = game:GetService("RunService")
local Player = game:GetService("Players")
--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------
local GetSuccess = "Fetched Data for %s"
local GetFail = "Something Went Wrong Getting Data for %s"
local SaveSuccess = "Saved Data for %s"
local SaveFail = "Something Went Wrong Saving Data for %s"
local NoData = "There is no Data for %s, Applying New Data!"
--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------
local DataStoreKey = "PlayerData" -- DataStore Key for Accessing Data
local StorePrefix = "Player_" -- Prefix Key for Getting Data
local MaxRetries = 3 -- How Many Times the Code will Retry if Failure
local Interval = 2 -- Wait Time when protected call Fails.
local StudioSaving = true -- Can DataStores work if Game is Run in Studio
local LoadOnce = false -- If DataStore will only Load Data once
local SaveOnce = false -- If DataStore will only Save Data once
local IsStudio = RunService:IsStudio() -- If Game is Running In Roblox Studio
local GameId = (game.GameId == 0) -- If GameId is 0 (Unpublished)
local SessionData = {} -- Data Holder, Makes it easier to Handle Data
local StarterData = { -- What Data New Players will Get when Joining
Level = 1;
Cash = 0;
EXP = 0;
Items = {
Weapons = {};
Tools = {};
};
Other = {
Banned = false;
Reason = "N/A";
};
}
--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------
This is just a Question as I’m hoping to Improve on this bit more, but if I cant, That’s ok!
It looks organized enough, but you mentioned that it’s a module script. I’d recommend adding your variables to a table, and then returning it so that other scripts can access it.
I didn’t show the Bottom half of the Module so I’ll show it, It mainly revolves around OOP so I’m not sure about adding Variables to the Table besides for metatables:
--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------
local DataStore
if IsStudio and not StudioSaving or GameId then
warn("Studio Saves Disabled or Game Unpublished, Data will NOT save!")
else
warn("DataStore API Accessed, Data will save!")
DataStore = DataStoreService:GetDataStore(DataStoreKey)
end
--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------
local UserData = {} -- Module Table
--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------
UserData.__index = UserData
--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------
function UserData.Get(p: Player)
local self = setmetatable({}, UserData)
self.Player = p
self.Key = StorePrefix..p.UserId
self.Data = table.clone(StarterData)
return self
end
--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------
function UserData:LoadProfile()
if IsStudio and not StudioSaving or GameId then return end
local Attempts = 1
local Success, Result
if LoadOnce then
Success, Result = pcall(DataStore.GetAsync, DataStore, self.Key)
else
repeat
Success, Result = pcall(DataStore.GetAsync, DataStore, self.Key)
Attempts += 1
if not Success then
task.wait(Interval)
end
until Success or Attempts > MaxRetries
end
if Success then
if Result then
self.Data = Result
print(string.format(GetSuccess, self.Player))
else
warn(string.format(NoData, self.Player))
end
else
warn(string.format(GetFail, self.Player))
self.Player:Kick("Something went wrong Getting your Data!")
end
end
--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------
function UserData:SaveProfile()
if IsStudio and not StudioSaving or GameId then return end
local Attempts = 1
local Success, Result
if SaveOnce then
Success, Result = pcall(DataStore.SetAsync, DataStore, self.Key, self.Data)
else
repeat
Success, Result = pcall(DataStore.SetAsync, DataStore, self.Key, self.Data)
Attempts += 1
if not Success then
task.wait(Interval)
end
until Success or Attempts > MaxRetries
end
if Success then
print(string.format(SaveSuccess, self.Player))
else
warn(string.format(SaveFail, self.Player))
end
end
--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------
return UserData
--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------