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!