How Should I Update Player's Stats?

Hello guys, so currently, I’m working on a player’s stats system that can save, can load and can update the stats.
Let’s go into the main topic! In the code below, I’m trying to make a function that can update player’s stats, but I’m lack of idea, so I’m making this topic to ask for you idea of how should I make a player’s stats update function!
The code :

PlayerStats.__index = PlayerStats
function PlayerStats:new(player)
	
	local stat = setmetatable({},self)
	
	stat.ID = 0
	stat.CD = false
	stat.Cash = 0
	stat.Blocking = false
	stat.currentCombo = 0
	
end

function PlayerStats:update(player,ID,CD,Cash,Blocking,currentCombo)
	
	
	
end

return PlayerStats

Thank you for reading! I hope you would spend a little bit of time looking at this topic!

Since you are using a method call, then one option could be to directly set the values.

function PlayerStats:update(player, new_id,CD,cash_amount,is_blocking,current_combo)
   self.ID = new_id or self.ID
   self.Cash = cash_amount or self.Cash
   self.Blocking = is_blocking or self.Blocking
   self.currentCombo = current_combo or self.currentCombo
end

(Using X or self.Y allows for nil values to be passed into the function parameters. If the value is nil, the value in the table will not be changed)

This is by no means the “best” solution, but it could work.
Hope this helps.
(Also I am not entirely sure why you are passing the player object as an argument since I don’t really see it being used?)

1 Like

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