i’m new to making custom services. this is the first custom service that i have made. i’ve researched into ways of programming aka DRY vs WET. (don’t repeat yourself vs write every time).
how’s this looking? would appreciate any tips or feedback! thanks
local CurrencyService = {}
function CurrencyService:GetCurrencyObject(player: Player, currencyName: string): IntValue?
local Leaderstats = player:WaitForChild("leaderstats")
if not Leaderstats then return end
return Leaderstats:WaitForChild(currencyName)
end
-- returns how much cash the player has
function CurrencyService:GetCurrency(player, currencyName)
local Currency = CurrencyService:GetCurrencyObject(player, currencyName)
if Currency then
return Currency.Value
end
end
-- adds player currency
function CurrencyService:AddCurrency(player, currencyName, amount)
local Currency = CurrencyService:GetCurrencyObject(player, currencyName)
if Currency then
Currency.Value = math.max(0, Currency.Value + amount)
end
end
-- sets player currency
function CurrencyService:SetCurrency(player, currencyName, amount)
local Currency = CurrencyService:GetCurrencyObject(player, currencyName)
if Currency then
Currency.Value = amount
end
end
function CurrencyService:ResetCurrency(player, currencyName, amount)
CurrencyService:SetCurrency(player, 0)
end
-- just a code example to test
game.Players.PlayerAdded:Connect(function(player)
CurrencyService:SetCurrency(player, "Blast Bucks", 44444444)
end)
-- RETURN --
return CurrencyService
First thing that i can see is the usage of methods for no reason at all. None of the functions use self at any point. Although i do think it makes calling the functions more fancy which is valid.
Secondly is also very minor: Not inverting conditions to reduce nesting.
You did this at the CurrencyService:GetCurrencyObject with the Leaderstats condition. But not with the other ones.
-- returns how much cash the player has
function CurrencyService:GetCurrency(player, currencyName)
local Currency = CurrencyService:GetCurrencyObject(player, currencyName)
if not Currency then return end
return Currency.Value
end
-- adds player currency
function CurrencyService:AddCurrency(player, currencyName, amount)
local Currency = CurrencyService:GetCurrencyObject(player, currencyName)
if not Currency then return end
Currency.Value = math.max(0, Currency.Value + amount)
end
And also comments is a big thing about making your custom services / modules, it helps a lot with your code maintainability, it is an essential habit to have. For this service its not that important because all functions are pretty self explanatory but when making more complex systems commenting both inside of a function and before it can be a life saver when you are weeks, months or even years into a project.
I would also recommend taking a look into OOP ( object oriented programming ) later. Its a very common way of making modules that uses of methods and metatables.
this looks nice, one thing i want to add using BindableEvents or Signals or some other library inside both of your SetCurrency and AddCurrecy functions after you change the amount
this way, you can listen for any change that happens to your currency and do stuff with it (i.e updating leaderstats values, etc.)