Hi, I wanna make a fully functional player stats, and i thought using OOP would be a great idea. The problem I tried to make a playersTable, where you can find every playerStats using the userId. So at the top of the script I created a local variable “local playersStatsTable = {}” the problem is that every time i require the script the playersStatsTable reset. I would love to know if there is a solution inside the module script without using some “outside help”. I was thinking of adding a bool value under the script to check if it was required at least once, but i thought it wasnt clean. Is there any cleaner solution?
local PlayerStats = {}
PlayerStats.__index = PlayerStats
local playersTable = {}
function PlayerStats.new(player : Player)
local self = setmetatable({}, PlayerStats)
-- Player
self.player = player
-- Base Stats
self.kills = 0
self.deaths = 0
-- Killing spree
self._killSpreeResetTask = nil
self.killSpreeTimer = 10
self.currentKillSpree = 0
self.maxKillSpree = 0
return self
end
function PlayerStats:updateMaxKillSpree()
if self.currentKillSpree > self.maxKillSpree then
self.maxKillSpree = self.currentKillSpree
end
end
function PlayerStats:updateKillSpree()
self.currentKillSpree += 1
self:updateMaxKillSpree()
print("Killing spree: " .. self.currentKillSpree)
-- Cancella il precedente reset se esiste
if self._killSpreeResetTask then
task.cancel(self._killSpreeResetTask)
end
-- Pianifica il reset dopo killSpreeTimer secondi
self._killSpreeResetTask = task.delay(self.killSpreeTimer, function()
self.currentKillSpree = 0
end)
end
function PlayerStats:addKill()
self.kills += 1
self:updateKillSpree()
end
function PlayerStats:addDeath()
self.deaths += 1
end
function PlayerStats:printData()
print(string.format("Kills: %s", self.kills))
print(string.format("Deaths: %s", self.deaths))
print(string.format("Current Kill Spree: %s", self.currentKillSpree))
print(string.format("Max Kill Spree: %s", self.maxKillSpree))
end
function PlayerStats.getPlayerByUserId(userId)
end
function PlayerStats.removePlayerByUserId(userId)
end
return PlayerStats