Can someone please optimize my "for i, v in" loops?

Here’s the the code:

local UserInputService = game:GetService("UserInputService")
local KeybindsFolder = game:GetService("ReplicatedStorage"):WaitForChild("Keybinds")

local buttonsArray = {}
local keybindButtonsArray = {}

for i, v in script.Parent.TopMiddleBar:GetChildren() do
	if v:IsA("TextButton") then
		table.insert(buttonsArray, v)
	end
end
for i, v in script.Parent.BottomMiddleBar:GetChildren() do
	if v:IsA("TextButton") then
		table.insert(buttonsArray, v)
	end
end

for i, v in buttonsArray do
	v.MouseButton1Click:Connect(function()
		if v:GetAttribute("selected") == true then
			return
		end
		for all, buttons in buttonsArray do
			buttons.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
			buttons:SetAttribute("selected", false)
		end
		v.BackgroundColor3 = Color3.fromRGB(200, 255, 200)
		v:SetAttribute("selected", true)
	end)
end

UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
	if gameProcessedEvent == true then
		return
	end
	for i, v in KeybindsFolder:GetChildren() do
		if v.Value == input.KeyCode.Name then
			
			local index = nil
			
			for all, buttons in buttonsArray do
				if buttons.Name == v.Name then
					print(all)
					index = all
				end
			end
						
			local targetButton = buttonsArray[index]		
			
			if targetButton:GetAttribute("selected") == true then
				return
			end
			for all, buttons in buttonsArray do
				buttons.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
				buttons:SetAttribute("selected", false)
			end
			targetButton.BackgroundColor3 = Color3.fromRGB(200, 255, 200)
			targetButton:SetAttribute("selected", true)
		end
	end
end)

Thanks!

1 Like

You can try to put some functions, so script will take less space:

local UserInputService = game:GetService("UserInputService")
local KeybindsFolder = game:GetService("ReplicatedStorage"):WaitForChild("Keybinds")

local buttonsArray = {}
local keybindButtonsArray = {}

local function collectTextButtons(parent, buttonsArray)
	for _, v in ipairs(parent:GetChildren()) do
		if v:IsA("TextButton") then
			table.insert(buttonsArray, v)
		end
	end
end

collectTextButtons(script.Parent.TopMiddleBar, buttonsArray)
collectTextButtons(script.Parent.BottomMiddleBar, buttonsArray)

local function resetButtonSelection(buttonsArray)
	for _, button in ipairs(buttonsArray) do
		button.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
		button:SetAttribute("selected", false)
		break
	end
end

local function setButtonSelected(button)
	button.BackgroundColor3 = Color3.fromRGB(200, 255, 200)
	button:SetAttribute("selected", true)
end

for _, v in ipairs(buttonsArray) do
	v.MouseButton1Click:Connect(function()
		if v:GetAttribute("selected") == true then
			return
		end
		resetButtonSelection(buttonsArray)
		setButtonSelected(v)
	end)
end

UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
	if gameProcessedEvent == true then
		return
	end
	for _, v in ipairs(KeybindsFolder:GetChildren()) do
		if v.Value == input.KeyCode.Name then
			for index, button in ipairs(buttonsArray) do
				if button.Name == v.Name then
					if button:GetAttribute("selected") == true then
						return
					end
					resetButtonSelection(buttonsArray)
					setButtonSelected(button)
					break
				end
			end
		end
	end
end)

1 Like

Thanks!

30 chаracters

1 Like

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