I would like to know if is a good habit place PlayerAdded and PlayerRemoving inside the module I do know that if I dont do require will not run the code below
---| Services |---
local Players = game:GetService('Players')
local PlayerData = {}
PlayerData.Players = {}
---| TYPE CHECK DONT TOUCH |---
type player_settings = {
Chance:number,
Character:string,
Skin:string
}
type player_methods = {
__index: player_methods,
}
export type player_data = typeof(setmetatable({} :: player_settings, {} :: player_methods))
---| ^ TYPE CHECK, DON'T TOUCH ^ |---
function PlayerData.new() : player_data
local self = setmetatable({}, {__index = PlayerData})
self.Chance = 0
self.Character = 'Test'
self.Skin = 'Default'
return self
end
---| Execute |---
Players.PlayerAdded:Connect(function(Player)
PlayerData.Players[Player.UserId] = PlayerData.new()
end)
Players.PlayerRemoving:Connect(function(Player)
PlayerData.Players[Player.UserId] = nil
end)
return PlayerData
export type player_data = typeof(PlayerData.new(table.unpack(...)))
for typechecking instead of manually defining types for player settings and methods
as PlayerData.New already returns the object you wanted to create a type for
aswell
types can be defined anywhere in the script so you can place them above or below the constructor function
module scripts are used to return a table or function that you can access with any script
so its better practice to put those events in the script thats requiring the module instead of the module itself
and just have .new() take in a player’s userid and insert the object into the table using the userid
local PlayerData = require('../Modules/Classes/PlayerData')
local Round = require('../Modules/Main/Round')
---| Services |---
local Players = game:GetService('Players')
---| Functions |---
local function PlayerAdded(Player:Player)
PlayerData.new(Player.UserId)
end
local function PlayerRemoving(Player:Player)
PlayerData.Players[Player.UserId] = nil
end
---| Execute |---
Players.PlayerAdded:Connect(PlayerAdded)
Players.PlayerRemoving:Connect(PlayerRemoving)