I want to make cleaner code

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

  1. Cleaner Code

  2. The Problem with this code it doesn’t look the best

  3. I’ve tried functions and putting parameters but this does make code look a bit cleaner but not clean enough

		button.MouseButton1Click:Connect(function()
			if button.Name == "Inventory" and button.Active then
				button.Active = false
				ClickTween(button.Parent.Parent)
				EnterTween(Inventoryname,InfoLeave,GoalLeave)
				task.wait(1.3)
				button.Active = true
				
			elseif button.Name == "Settings" and button.Active then
				button.Active = false
				ClickTween(button.Parent.Parent)
				EnterTween(Settingsname,InfoLeave,GoalLeave)
				task.wait(1.3)
				button.Active = true
				
			elseif button.Name == "Shop" and button.Active then
				button.Active = false
				ClickTween(button.Parent.Parent)
				EnterTween(Shopname,InfoLeave,GoalLeave)
				task.wait(1.3)
				button.Active = true
			end
		end)
	end
end
2 Likes

You are repeating a lot of this code, which could be put outside of the if statement.

button.MouseButton1Click:Connect(function()
            if not button.Active then return end
            button.Active = false
			ClickTween(button.Parent.Parent)
			if button.Name == "Inventory" then
				EnterTween(Inventoryname,InfoLeave,GoalLeave)
			elseif button.Name == "Settings" then
				EnterTween(Settingsname,InfoLeave,GoalLeave)
			elseif button.Name == "Shop" then
				EnterTween(Shopname,InfoLeave,GoalLeave)
			end
            task.wait(1.3)
		    button.Active = true
		end)
3 Likes
for _, button in pairs(script.Parent.Parent.LeftMiddle:GetDescendants()) do
	if button:IsA("TextButton") then
		
		button.MouseEnter:Connect(function()
			if button.Name == "Inventory" then 
				EnterTween(Inventoryname,InfoEnter,GoalEnter)
				
			elseif button.Name == "Settings" then
				EnterTween(Settingsname,InfoEnter,GoalEnter)
				
			elseif button.Name == "Shop" then
				EnterTween(Shopname,InfoEnter,GoalEnter)
				
			end
		end)
		
		--[[ Mouse Exit Functions ]]--
		
		button.MouseLeave:Connect(function()
			if button.Name == "Inventory" then
				EnterTween(Inventoryname,InfoLeave,GoalLeave)
				
			elseif button.Name == "Settings" then
				EnterTween(Settingsname,InfoLeave,GoalLeave)
				
			elseif button.Name == "Shop" then
				EnterTween(Shopname,InfoLeave,GoalLeave)
				
			end
		end)``` Does this look okay?