Script not working how I want

local currentTab = 1

local function setUpText()
	local buildingData = (currentTab == 1) and module.barracks or module.uttility

	for i = 1, 4 do
		local button = buildingFrame1:FindFirstChild("Building" .. i)
		if button and button:IsA("ImageButton") then
			local buildingName = button:FindFirstChild("BuildingName")
			local building = buildingData["Building" .. i]

			if building then
				buildingName.Text = building.Name

				button.MouseEnter:Connect(function()
					buildingName.Text = "$" .. building.Price
				end)
				button.MouseLeave:Connect(function()
					buildingName.Text = building.Name
				end)
			else
				buildingName.Text = "No Building"
			end
		end
	end
end

this is the code that changes tabs:

local function switchTab(tabNumber)
	currentTab = tabNumber
	setUpText() -- Update button texts when tab changes
end

frame.Tab1Button.MouseButton1Click:Connect(function()
	switchTab(1)
end)

frame.Tab2Button.MouseButton1Click:Connect(function()
	switchTab(2)
end)

So when I change tabs the text does display the right names for each button, the thing is when I am on tab 1 and hover an button it will get the button number and go inside the module to get all the info for the correspondant number
example

	["Building1"] = {
			Name = "Normal Barrack",
			Price = 50,
			model = buildingsFolder.Barracks.NormalBarrack
		};

Tho once I hover off the button it should go back to showing the name instead of the price. Tho if I press on Tab 1 it works fine, once I go into tab 2, it displays the text on click but once I hover off it shows the tab 1 names

When you recall setUpText(), you aren’t cleaning up the old connections. The old one attached to currentTab 1 might still exist and interrupt the newer connections. To clean up the old ones, disconnect them in some way. You may need a reference to the connections to disconnect them.

Alternatively, I recommend just making the buttons search for the buildingData based on the currentTab since it’s above the scope of the function (global).

1 Like

How would I discconect them, and isnt the script already searching for the buildingData tho?

You make a variable equal to the :Connect() calls you made then you call :Disconnect() on it.

Here’s another way you can do this which may be a little more efficient, create a data structure (some accessible table) that holds the current building data. You update this everytime you change tabs but you don’t renew the button connections. The MouseEnter and MouseLeave connections then read that data structure which can change when tabs change.

Why? So you don’t make new connections everytime you call setUpText(). It’ll be a little better.

Try to flowchart what I’m saying if it’s confusing.

I would go with the disconnect(), tho what exactly should I disconnect or how?

Disconnect the connections when you call setUpText(), then reconnect them. The first time around there are no connections, simply check if the connections were made so you don’t disconnect nil.

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