OOP is broke...?

so, this is getting me really mad, no error BUT I DO NOT UNDERSTAND what’s wrong, xd, basically:

local Players = game:GetService('Players')
local Combat = require(game.ReplicatedStorage.CombatSystem.Swords)
local Methods = {}
Players.PlayerAdded:Connect(function(Player)
	local char = Player.Character or Player.CharacterAdded:Wait()
	
	local met = Combat.new(Player,{UpperTorso = 10,HumanoidRootPart=10},game.ReplicatedStorage.CombatSystem.Animations.xdddd,char)
	Methods[Player.UserId] = met
	wait(10)
	Combat:Equip('Basic Sword',8)
end)

^ Just test :slight_smile:


But when I do this:

Or when I do this:
print(self.Player.Character)
Says self.Player is nil…

Make sure Swords is indexing its self.

Swords = {}
Swords.__index = Swords

Yes, I did that!



Pretty sure!!

Try writing it as

 local self = setmetatable( {}, Swords)

 ...

 return self

?
but, how i put the values in it?

self[index] = value

or self.index = value

Nope, still same error

I don’t understand that?
Will that work?
EDIT: Different error now
image

Yes it will work, your setting self.Player to be equal to the value “player” passed in as a parameter

Then change that to

Swords.Player.Character.RightHand

Obviously since you assigned the player index under the Swords table, you’ll need to index it from there.

Uh, I mean, you’re prob right, but… why would this work?
I mean Swords is a table, I shouldn’t do self.Player as I did before?
Why did it didn’t worked before tho

Same error:


image

btw:

You defined self explicitly as something else, you could change your method to

function Swords:func(...) end

to use self to reference the table.

Sure you called Swords.new() before accessing the player from the other function?
If you didn’t the index wouldn’t have been defined.

I don’t understand, anyway, why didn’t self.Player = player worked?
I mean, it should, I think there’s nothing wrong with that as * far as I know*

Why should it?
You could simplify several things in there to make it easier to debug, self didn’t work there because you defined self's value as a table, if you define this function:

image

as

function Swords:new(player, ...)

then self would reference the Swords table within that block, so you could then just do

self.Player = player

OOP is not broken.

I see the problem now. Its really simple. You set the object to a variable named met. You should be calling the method through that variable, aka met:Equip()

i don’t know if this would help but self.Player is just nil and you referenced it in other functions but if you reference it on the entire module it might work

:man_facepalming:
How I didn’t noticed!! lol!!