How should I organize this ModuleScript?

Hi, Although this doesn’t involve Scripting, and should probably be in #help-and-feedback:game-design-support or #help-and-feedback:art-design-support , I figured since it has to do with a Script, I might as well put it here.

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!

Anything helps,

Thanks.

This looks good! The only thing I have to say is there is a special category in the devforum called code review for these kinds of topics

I’d also recommend adding them to the module table so other scripts can access them.

1 Like

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
--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------

I’m not a professional scripter, but that looks organized enough for me.

1 Like

Thanks, I was just wondering if it was ok.

It is alright as long as you can easily navigate in it. That’s what matters

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.