Help with Custom Functions

Well, I was working on my module that handles ProfileService and I need help about custom functions. So one day, when I was just about to finish up my module and working on my other scripts, I kind of wanted to make custom functions and signals for better performance. So i made a Module:GetProfileOfCharacter(player) function that returns the profile of the player with custom functions and I did this:

--first lines of code 
		function playerProfile:Increment(statName, increment, operation)
			if playerProfile.Data[statName] ~= nil then
				if type(increment) == "number" then
					if operation ~= nil then
						if string.lower(operation) == "mul" then
							playerProfile.Data[statName] *= increment
							DataManager.ProfileUpdated:Fire(playerProfile, player, playerProfile.Data)
						elseif string.lower(operation) == "div" then
							playerProfile.Data[statName] /= increment
							DataManager.ProfileUpdated:Fire(playerProfile, player, playerProfile.Data)
						end
					else
						playerProfile.Data[statName] += increment
						DataManager.ProfileUpdated:Fire(playerProfile, player, playerProfile.Data)
					end
				elseif type(increment) == "boolean" or type(increment) == "string" then
					print(statName, increment)
					playerProfile.Data[statName] = increment
					DataManager.ProfileUpdated:Fire(playerProfile, player, playerProfile.Data)
				else
					error("[Data Manager]: Invalid increment type. If you are trying to insert a data to a table, Use Profile:InsertToTable()")
				end
			else
				error(("[Data Manager]: Invalid profile data name. Does %s exist?"):Format(statName), 2)
			end
		end
		
		function playerProfile:InsertToTable(statName, data, keyName: any?)
			if playerProfile.Data[statName] ~= nil and type(playerProfile.Data[statName]) == "table" then
				if keyName ~= nil then
					playerProfile.Data[statName][keyName] = data
					DataManager.ProfileUpdated:Fire(playerProfile, player, playerProfile.Data)
				else
					table.insert(playerProfile.Data[keyName], data)
					DataManager.ProfileUpdated:Fire(playerProfile, player, playerProfile.Data)
				end
			else
				error("[Data Manager]: Profile data is nil or isn't a table", 2)
			end

		end
		
		
		return playerProfile
--rest of the code

The reason im not defining playerProfile.Data[statName] is because the function doesnt change the data of the profile when I set a variable, Heres an example:

local profileData = playerProfile.Data[statName]
profileData += increment --Doesn't change!

Is there any way I can optimise this? I don’t really know how to implement metatables here and I’m really confused. Any feedback will be appreciated!

local profileData = playerProfile.Data[statName]
profileData += increment --Doesn’t change!

This doesn’t change because you are setting the value of playerProfile.Data[statName] to profileData, not a “reference” to the value in the table. profileData += increment in this case increases the value of the variable profileData by increment, not playerProfile.Data[statName].

For example, if playerProfile.Data[statName] is 1 and increment is 1, this it what your code would become:

local profileData = 1
profileData += 1
print(playerProfile.Data[statName], profileData) -- 1, 2

As you can see, the value in playerProfile.Data is never modified, only the variable profileData. On the other hand, if we don’t localize it:

playerProfile.Data[statName] += 1
print(playerProfile.Data[statName], profileData) -- 2

Your code isn’t an optimization and is actually changing the way the code functions (no need to optimize it anyway since that’s about as fast as that assignment is going to be).

1 Like

Thanks for the advise. As for the custom functions, I actually got a way to make it work with metatables and used a module.