Adding variables to my function inside of a table

Im trying to make a script that runs a in pairs loop to make buttons do functions but then a smaller size

heres my problem if I add () to put the variables in it will run the code and start an error so I cant do that but I also dont want to make more functions just to change a few things inside of it. Im using tables to keep it shorter

local function ChosenMap(title, button, back)
	ChosenTitle1.Text = title
	ChosenTitle2.Text = title
	button.UIStroke.Color = Color3.new(0.333333, 0.666667, 0.498039)
	button.ImageColor3 = Color3.new(0.333333, 0.666667, 0.498039)
	back.UIStroke.Color = Color3.new(0, 0, 0)
	back.ImageColor3 = Color3.new(1, 1, 1)
end

local button1Ups = {
	[StartButton] = StartGame,
	[TutorialButton] = ChosenMap
	
}

thats a small part of the code that you might need for a solution keep in mind that I cant add () to the chosenmap to choose a value for the title, button or back

Run the code inside the loop with ()

for button, func in pairs(button1Ups) do
   func("Title", button, arg3)
end

yeah but if I make a in pairs loop I do it like this

for k, v in pairs(button1Ups) do
	k.MouseButton1Up:Connect(v)
end

which means it will run the value but in your case it isnt linked to a certain button but rather to all of them

for k, v in pairs(button1Ups) do
	k.MouseButton1Up:Connect(v, "Title",button,arg3)
end

Check that code again.

My bad it’s a bit late

for k, v in pairs(button1Ups) do
	k.MouseButton1Up:Connect(function()
              k("Title", button, arg3)
        end)
end
1 Like

yeah but I still dont get it sorry how is the script supposed to know to which function this info is linked or am I sopped to keep the [button] = DeployToGame and then its linked to that function? bc not all my buttons are supposed to have the same function I just use it to make the script shorter and I supose i can use a if statement but still then I have to change a lot to make that work

I don’t understand what you mean by that? If you need extra data to pass in, you can turn your table into a nested table.

local button1Ups = {
	[StartButton] = {StartGame, "Title", StartButton, "Hello!"},
	
}
for button, data in pairs(button1Ups) do
	local func, title, button2, arg3 = table.unpack(data)
    func(title, button2, arg3)
end