I have watched a few tutorials about Object Oriented Programming and did not get the hang of it but I made my attempt to code something using it and I get a few errors, I tried following the steps of a module but yet I still failed and now I get an error,
local Players = game:GetService("Players")
local FolderHandler = {}
FolderHandler.__index = FolderHandler
local function doSomething()
print("lol")
end
function FolderHandler.New(Player: Player)
local self = {}
setmetatable(self, FolderHandler)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = Player
self:CreateValue(doSomething())
return leaderstats
end
return FolderHandler
Server:
local handler = require(game:GetService("ReplicatedStorage").ModuleScript)
local players = game.Players
players.PlayerAdded:Connect(function(player)
local leaderstats = handler.New(player)
leaderstats:CreateValue()
end)
this piece of code may make some of you laugh but at the very end I’m just trying to learn so if with time in their hands are able to explain to me what I did wrong I would be very thankful.
I’m assuming here you were trying to make a function that uses a previously stated local function. In this case you should try doing this instead:
function self:CreateValue()
doSomething()
end
my next point is that you are returning a folder instead of the self object, hence you are attempting to call a function on a folder. Instead you should return self and put the leaderstats folder inside the self object. This is the fully adjusted code:
local Players = game:GetService("Players")
local FolderHandler = {}
FolderHandler.__index = FolderHandler
local function doSomething()
print("lol")
end
function FolderHandler.New(Player: Player)
local self = {}
setmetatable(self, FolderHandler)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = Player
self.leaderstats = leaderstats
function self:CreateValue()
doSomething()
end
return self
end
return FolderHandler