OOP Module Issue

Hi Devs, How come this return nil?

local GuiControl = {}
GuiControl.__index = GuiControl
GuiControl.GuiDebounce = false

function GuiControl.new(guis: ScreenGui)
	local self = setmetatable({}, GuiControl)
	
	self.Guis = guis
	self.Boards = self.Guis:FindFirstChild("Boards")
	self.InterMission = self.Guis.Intermission
	self.Counter = self.InterMission.TextLabel
	self.Counter.Text = "Intermission: "..Status.Value
	Status.Changed:Connect(self.Changed, self)
	
	task.spawn(function()
		for i, v in next, self.Guis:GetChildren() do
			if v:IsA("ImageButton") then

				v.MouseEnter:Connect(function()
					if v.Name == "PlayerButton"  then return end


					self:Hover(v, v.Name)
				end)

				v.MouseLeave:Connect(function()
					if v.Name == "PlayerButton" then return end


					self:UnHover(v, v.Name)
				end)

				v.Activated:Connect(function()
					if v.Name == "PlayButton" then
						--GuiItils[v.Name].Functionality(self, v, v.Name)
						task.spawn(GuiItils[v.Name].Functionality, self, v, v.Name)
					else
						task.spawn(self.TweenGui, self, v, v.Name)
						self:OpenBoard(self.Boards[v:GetAttribute("Board")], v:GetAttribute("Board"))
					end
				end)
			end
		end
	end)

	return self
end

function GuiControl:Changed()
	if self.Counter then
		print("Text Label Exist")
	else
		print("Not Exist")
	end
	--self.Counter.Text = "Intermission: "..Status.Value
end

image
ignore the “GOT CALLED”

i found it finally

local GuiControl = {}
GuiControl.__index = GuiControl
GuiControl.GuiDebounce = false

function GuiControl.new(guis: ScreenGui)
	local self = setmetatable({}, GuiControl)

	self.Guis = guis
	self.Boards = self.Guis:FindFirstChild("Boards")
	self.InterMission = self.Guis.Intermission
	self.Counter = self.InterMission.TextLabel
	self.Counter.Text = "Intermission: "..Status.Value
	
	function self:Changed()
		if self.Counter then
			print("Text Label Exist")
		else
			print("Not Exist")
		end
		--self.Counter.Text = "Intermission: "..Status.Value
	end
	
	Status.Changed:Connect(function()
		self:Changed()
	end)

	task.spawn(function()
		for i, v in next, self.Guis:GetChildren() do
			if v:IsA("ImageButton") then

				v.MouseEnter:Connect(function()
					if v.Name == "PlayerButton"  then return end


					self:Hover(v, v.Name)
				end)

				v.MouseLeave:Connect(function()
					if v.Name == "PlayerButton" then return end


					self:UnHover(v, v.Name)
				end)

				v.Activated:Connect(function()
					if v.Name == "PlayButton" then
						--GuiItils[v.Name].Functionality(self, v, v.Name)
						task.spawn(GuiItils[v.Name].Functionality, self, v, v.Name)
					else
						task.spawn(self.TweenGui, self, v, v.Name)
						self:OpenBoard(self.Boards[v:GetAttribute("Board")], v:GetAttribute("Board"))
					end
				end)
			end
		end
	end)

	return self
end

put the changed function inside the new function

1 Like

You were right about how the :Connect should be, but you do not need to define Changed inside the constructor, as this is bad for memory usage.

3 Likes

yeah i’ve seen that on an OOP tutorial

My suggestion is to print more to find the error.

thanks tho but i have solve the problem

That is not correct. obj:method() is the same as obj.method(obj), the former just implicitly passes self whereas the latter explicitly passes it.

as @Professor_Boxtrot said

1 Like

Sure you can pass it manually, what I meant is that it isn’t passed automatically when using .

self.function() --does not pass self
self:function() --does pass self
1 Like

If the problem is solved you should select the solution (your own comment) or post the solution and select it, so the thread will close.

so guys this is what i did put the status on a self.Status then did this:

	self.Text = self.Guis:FindFirstChild("Intermission").TextLabel
	self.Status = ReplicatedStorage:WaitForChild("Status")
	
	self.Text.Text = "Intermission: "..self.Status.Value
	self.Status.Changed:Connect(function(str)
		self.Text.Text = "Intermission: "..self.Status.Value
	end)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.