Need assistance on this Metatable

The function Items:ButtonHandler is being called but the issue seems to be the the button:MouseButton1Click isn’t working.

And yes their is a button and when i print (Button) it gets the button the issue is that the button:MouseButton1Click is both not printing the print(“Called”) and is not working or seems to be buggin as i have tried varies troubleshooting methods and do not understand why the button:MouseButton1Click isn’t working.

If any of you can help me out it will be appreciated.

local Items = {}
Items.__index = Items

function Items:new(Boards, HoverBoardGui, H_ViewPortFrame)
	local self = setmetatable({}, Items)

	--//Instances
	self.BoardsFolder = Boards
	self.HoverBoardGuiScreen = HoverBoardGui
	self.AssetVPR = H_ViewPortFrame
	self.SelectButton = self.AssetVPR.SelectButton

	for _, v in self.BoardsFolder:GetChildren() do 
		--//Variables
		local SelectFrame = self.HoverBoardGuiScreen.Frame.Select 
		local InfoFrame = self.HoverBoardGuiScreen.Frame.Info
		local ScrollingFrame = SelectFrame.ScrollingFrame
		local Camera = Instance.new("Camera", self.AssetVPR.ViewportFrame)
		local VprF_C = self.AssetVPR:Clone()
		local V_C = v:Clone()
		local A_ = 0
		
		--//Constant
		VprF_C.Parent = ScrollingFrame
		VprF_C.ViewportFrame.CurrentCamera = Camera
		V_C.Parent = VprF_C.ViewportFrame
		
		--//Connecting Function
		self:CourtineHandler(A_, Camera, V_C)
		self:ButtonHandler(self.SelectButton, self.BoardsFolder)
	end
	
	return self
end

function Items:CourtineHandler(A_, Camera, V_C)
	--//CoroutineWrapFunction
	coroutine.wrap(function()
		while wait() do
			A_ += 1
			Camera.CFrame = V_C.ViewPart.CFrame * CFrame.Angles(0, math.rad(A_), 0) * CFrame.new(0, 1.7, 						V_C.ViewPart.Size.Z * 1) * CFrame.Angles(math.rad(-15), 0, math.rad(-37))
		end
	end)()
end

function Items:ButtonHandler(Button, Boards)
	print(Button)
	--//Const
	Button.MouseButton1Click:Connect(function()
		print("Called")
		Items:GiveItem(Boards)
	end)
end

function Items:GiveItem(_Boards)
	print("Items:GiveItem function called")
	--//Const
	for _, Board in (_Boards:GetChildren()) do
		local BoardClone = Board:Clone()
		BoardClone.Parent =  Player.Backpack
		if BoardClone.Parent ~=  Player.Backpack then
			print("Player has "..BoardClone.Name.." in his bag")
		end
	end
	
end

return Item
1 Like

May I ask why you’re not using MouseButton1Up, and whether or not MouseButton1Up works?

Some other advice:

	self:ButtonHandler(self.SelectButton, self.BoardsFolder)

This is in a for loop, so you are doing the same thing several times with the same parameters. Is this intentional? Perhaps this is meant to be outside the loop, or perhaps you are meant to use different parameters?

function Items:CourtineHandler(A_, Camera, V_C)
	--//CoroutineWrapFunction
	coroutine.wrap(function()
		while wait() do
			A_ += 1
			Camera.CFrame = V_C.ViewPart.CFrame * CFrame.Angles(0, math.rad(A_), 0) * CFrame.new(0, 1.7, 						V_C.ViewPart.Size.Z * 1) * CFrame.Angles(math.rad(-15), 0, math.rad(-37))
		end
	end)()
end

there is a thing similar to .wrap but doesnt need () at the end:

function Items:CourtineHandler(A_, Camera, V_C)
	--//ThreadCreationFunction
	task.spawn(function()
		while wait() do
			A_ += 1
			Camera.CFrame = V_C.ViewPart.CFrame * CFrame.Angles(0, math.rad(A_), 0) * CFrame.new(0, 1.7, 						V_C.ViewPart.Size.Z * 1) * CFrame.Angles(math.rad(-15), 0, math.rad(-37))
		end
	end)
end

using task.wait() instead of wait() will also run the loop faster, but if its fast enough then that’s not a problem. task.wait() is also more accurate.

You can also probably maybe use self. here too if the parameters are always the same:

function Items:ButtonHandler()
	print(self.Button)
	--//Const
	self.Button.MouseButton1Click:Connect(function()
		print("Called")
		self:GiveItem(self.Boards) --you can also keep the parameters empty here too () and use self.Boards in :GiveItems instead
	end)
end