Running the same function for multiple buttons

I am trying to script a menu as a showcase of my skill, one of the tabs has a lot of buttons, each one looking the same and doing the same thing basically and it’s called the same, now I am trying to run a function for all of these buttons in one script so in case I need to change something I only need it to run once

here is the function

function module.buttonHover(button) -- tween button when mouse is hovering
	local buttonPos = button.Position
	local tweenInfo = module.getDefaultTweenInfo(0.1)
	local hoverPosition = buttonPos + UDim2.new(0, 0, 0, 3)

	local hoverTween = tweenService:Create(button, tweenInfo, {Position = hoverPosition})
	local backTween = tweenService:Create(button, tweenInfo, {Position = buttonPos})

	button.MouseEnter:Connect(function()
		hoverTween:Play()
	end)

	button.MouseLeave:Connect(function()
		backTween:Play()
	end)
end

and here’s the local script

local mainFrame = script.Parent.mainFrame
local buttons = mainFrame.Buttons

for _, child in pairs(mainFrame:GetChildren()) do
	if child.Name == "inventoryFrame" then
		ModFunctions.buttonHover(child)
	end
end

I am not getting any errors and when I use the function outside of the for loop it works but only for one of the buttons

do all the buttons having the name “inventoryFrame”?

1 Like

yup

the name check is case sensitive

1 Like

even if I change the if statement to IsA() it still doesn’t work

okay can you add a print() in the function to see if it processed all those children?
may also add print inside the button hover event

1 Like

Alright, turns out that I wasn’t giving the correct information, I was doing

mainFrame:GetChildren()

instead of

mainFrame.Inventory:GetChildren()
1 Like

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