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
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
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.
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
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)
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.