Weird Metatable bug while using self in function

guys i need help with an equip script so like its a drag and drop one and i cant have a local script inside each item textbutton i decided to use metatables

local Weapon = {}
Weapon.__index = Weapon
function Weapon:MouseButton1Down()
    print("MouseDown")
    if self.Object.MouseButton1Down() then
        self.Object.Position = UDim2.new(0,mouse.X,0,mouse.Y)
        if self.Object.Parent.Parent.Weapon1.MouseEnter() then
            self.Object.Parent.Parent.Weapon1.Text= self.Object.Name
        end
    end
end
function Weapon:Remove()

    self.connection:Disconnect()
    self.connection = nil
self = nil
end
Weapon.new = function(obkject)
    local self = {}
    setmetatable(self,Weapon)
    self.Object = obkject
    self.connection =  obkject.MouseButton1Down:Connect(self.MouseButton1Down)
    print("we did it")
    weaponuisssss[obkject] = self
end

image
and basically my life is hell
weirdest bug i have encountered… (i learnt metatables recently so pls tell any tips if u have any)
edit: mains solution MAIN SOLUTION

you need to return self at the end of your Weapon.new constructor.

3 Likes

MouseButton1Down (the event) passes two numbers, the x and y position of the mouse, to the function connected to it. Your function uses self, which would be the x position of the mouse since the function is not being passed the object implicitly like colon syntax would.

2 Likes

oh so self is passed as the first argument automatically and since there were other arguments tht i didnt account for it replaced self with tht?? so now do i like do-

function Weapon:MouseButton1Down(x,y)
    print("MouseDown")
    if self.Object.MouseButton1Down() then
        self.Object.Position = UDim2.new(0,mouse.X,0,mouse.Y)
        if self.Object.Parent.Parent.Weapon1.MouseEnter() then
            self.Object.Parent.Parent.Weapon1.Text= self.Object.Name
        end
    end
end

or smthg like


function Weapon:MouseButton1Down(mousex,mousey,self)
    print("MouseDown")
    if self.Object.MouseButton1Down() then
        self.Object.Position = UDim2.new(0,mouse.X,0,mouse.Y)
        if self.Object.Parent.Parent.Weapon1.MouseEnter() then
            self.Object.Parent.Parent.Weapon1.Text= self.Object.Name
        end
    end
end

?

1 Like

The function definition is fine. x:y() is shorthand for x.y(x). When you connect it to the event, though, the event does x.y(unrelated args), meaning self is defined as something else. Here, it’s the x coordinate of the mouse. You can just connect a function that calls it properly to the event, like this:

obkject.mousebutton1down:Connect(function(...)
	self:MouseButton1Down(...) -- colon syntax means self is passed
end)
2 Likes

thx tht worked ima keep it in mind but like another thing popped up


image

That’s because you passed the result of the function being called (what it returns) rather than an anonymous function that calls it.

2 Likes

confused i suck at scripting noises… so from wht i understood u mean like i passed self:MouseButton1Down() instead of self:MouseButton1Down or smthg ??

MouseButton1Click is an event. It will pass x, y to the function that is connected. You want self passed instead. The way to do this is connecting another function that will call MouseButton1Click with self. You can just do whatever:Connect(function() --[[ do stuff here ]] end) instead of actually writing out a function that’s only going to have one line. If you inline the function like that, so it has no variable that stores it, it’s what we call an “anonymous” function. In my other post, I used an anonymous function to call your MouseButton1Click function with the colon, since using a colon makes self be passed.

Notice that I have function(...) and end in between the parentheses of Connect. I’m making a new function and putting it directly into the connection. That function will be called when the event fires, and that function will call MouseButton1Click and pass self.

2 Likes

did tht and am back to bug 1 at this point i feel like making a table containing all the selfs and stuff…