OOP is great to learn, I think you want something like this:
Module Script:
local module = {}
module.__index = module
function module.new(player)
local playerData = {
player = player;
health = 100;
}
setmetatable(playerData, module)
return self
end
function module:AddHealth(health)
self.health = self.health + health
end
return module
And in your script, you’d have something like this:
local mod = require(script.whatever)
game.Players.PlayerAdded:Connect(function(p)
local player = mod.new(p)
player:AddHealth(10)
end)
To add on, it doesn’t seem like a very constructive use of OOP, all of this could be done with just normal events.
You really only want a player object if you’re keeping track of something, I use OOP for things like combat heals, when I want to detect if a heal on a player is triggered from combat.
Yes, I kind of wanted to know how to add an object, not gonna use that just to make my script looks more professional lol, but, I didn’t understand what
.__index I guess it’s a methametod but what it does? And why self doesn’t works without it
Also, sorry, i’m asking you a lot, but I just want to learn about, it has been difficult for me to learn it lol
local module = {}
module.__index = module
function module:NewPlayer(player)
local PlayerData = {
player = player
}
local MetaTable = setmetatable(PlayerData, module)
return PlayerData
end
function module:AddHealth(health)
self.Player.Character.Humanoid.Health = self.Player.Character.Humanoid.Health - health
end
return module
o didn’t noted that, well, thank you so much, O it didn’t worked lol
function module:NewPlayer(ok)
local PlayerData = {
player = ok
}
local MetaTable = setmetatable(PlayerData, module)
return PlayerData
end
function module:AddHealth(health)
self.player.Character.Humanoid.Health = self.player.Character.Humanoid.Health - health
end
Where do you call NewPlayer? You’re probably calling it with a period instead of a colon or something. @Meta_data. The name of the function doesn’t matter, it’s the fact he’s defining it as a method.
local mod = require(game.ReplicatedStorage.OOP.ModuleScript)
game.Players.PlayerAdded:Connect(function(p)
local player = mod.NewPlayer(p)
mod:AddHealth(100)
end)
The issue is the fact you are calling it with the . notation.
a:b(...) is syntax sugar (a nicer way to write something) for a.b(a, ...).
Define the function with . notation and call it with . notation. The newPlayer function is not actually meant to be used as a method here, but rather as a constructor. A constructor defines how to create an instance of a class.
Tbh I wouldn’t even use OOP here. You are not using it for anything big here, it’s only overcomplicating something as simple as subtracting health.
NewPlayer is a method. your using a “:” when you defined the function in the module. But in the script that your creating the object, you are using “.”
local mod = require(game.ReplicatedStorage.OOP.ModuleScript)
game.Players.PlayerAdded:Connect(function(p)
local player = mod:NewPlayer(p)
mod:AddHealth(100)
end)
Did you change the module script at all? That should work fine. If you define both as a method or both as a constructor, it doesn’t really matter which; it’s just a styling issue.