Feedback on my custom currency service

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

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

It looks good!

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.

If you don’t know what methods are you can look here: Functions | Documentation - Roblox Creator Hub

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.

3 Likes

thank you so much for your help junho! i made changes based off of what you said. :slight_smile:

i usually use methods because as you said it’s just a little fancy and i’m used to using them tbh but i understand your point completely.

that was a great point and i made those changes. thank you for the code example.

definitely. i usually add comments in my script before programming so i know what functions i have to code. :slight_smile:

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

1 Like

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