Scripting ui Better help?

You can write your topic however you want, but you need to answer these questions:

  1. I’m trying to make it so i don’t have to script each of those buttons one by one a more better way to do it but now i’m having a issue

  2. the issue is When i click the button it print 8x and you can see why that can be a issue

for _, button in pairs(script.Parent.Parent.LeftMiddle:GetDescendants()) do
	if button:IsA("TextButton") then
		button.MouseEnter:Connect(function()
			if button.Name == "Inventory" then
				print("InventoryHover")
			elseif button.Name == "Settings" then
				print("SettingsHover")
			elseif button.Name == "Shop" then
				print("ShopHover")
			end
		button.MouseButton1Click:Connect(function()
			if button.Name == "Inventory" then
				print("Inventory")
			elseif button.Name == "Settings" then
				print("Settings")
			elseif button.Name == "Shop" then
				print("Shop")
			end
		end)
		end)
	end
end

You’re looping through each and every button with that for loop. You’re better off scripting them individually.

This is most likely happening because you have the button.MouseButton1Click:Connect() inside the loop. so you connect the function as many times as the loop.

Isn’t there a better way of scripting this There has to be

Use this:

	if button:IsA("TextButton") then
		button.MouseEnter:Connect(function()
			if button.Name == "Inventory" then
				print("InventoryHover")
			elseif button.Name == "Settings" then
				print("SettingsHover")
			elseif button.Name == "Shop" then
				print("ShopHover")
			end
		end)
		button.MouseButton1Click:Connect(function()
			if button.Name == "Inventory" then
				print("Inventory")
			elseif button.Name == "Settings" then
				print("Settings")
			elseif button.Name == "Shop" then
				print("Shop")
			end
		end)
	end
end
1 Like

Sorry if it doesnt show the whole code idk why its not showing it, may be a a dev forum bug.