So I’m still pretty new to OOP. I’m trying to make a PlayerModule using it. I wrote the following code:
local playersService = game:GetService('Players')
local serverScriptService = game:GetService('ServerScriptService')
local modules = serverScriptService.Modules
local dataStore2 = require(modules.DataStore2)
local playerModule = {}
playerModule.__index = playerModule
dataStore2.Combine('DATA','coins')
function playerModule.new(player)
local self = setmetatable({},playerModule)
self.dataStores = {
Coins = 'coins'
}
self.player = player
return self
end
function playerModule:PlayerAdded()
-- if there isn't a player then return with a warning
if not self.player then
return warn('Player not found')
end
-- add the leaderstats, coins, etc.
local coinsStore = dataStore2('coins',self.player)
local leaderstats = Instance.new('Folder')
leaderstats.Name = 'leaderstats'
local coins = Instance.new('NumberValue')
coins.Name = 'Coins'
coins.Value = coinsStore:Get(0)
coins.Parent = leaderstats
coinsStore:OnUpdate(function(newValue)
coins.Value = newValue
end)
leaderstats.Parent = self.player
end
return playerModule
Script:
local playersService = game:GetService('Players')
local replicatedStorage = game:GetService('ReplicatedStorage')
local serverScriptService = game:GetService('ServerScriptService')
local modules = serverScriptService.Modules
local playerModule = require(modules.PlayerModule)
playersService.PlayerAdded:Connect(function(player)
local maid = require(modules.Maid).new()
local newPlayer = playerModule.new()
newPlayer:PlayerAdded()
end)
“Player Not Found” prints. But why? I’m defining self.player!