Does this play the function?

I’m new to scripting and I am learning more about how functions work. Now I know how to do simple functions like:

game.TextButton.MouseButton1Click:Connect(function()
    print("Button Clicked!")
end)

The function above prints “Button Clicked” when the button is clicked, obviously.

But as I was learning more about functions I saw something like this:

local button = game.StarterGui.ScreenGui.TextButton

local function ButtonClicked()
    print("Button Clicked!")
end

button.MouseButton1Click:Connect(ButtonClicked)

My question is, the button.MouseButton1Click:Connect(ButtonClicked) runs the function, right? Also if I duplicate it will it run multiple times? Like this:

local button = game.StarterGui.ScreenGui.TextButton

local function ButtonClicked()
    print("Button Clicked!")
end

button.MouseButton1Click:Connect(ButtonClicked)
button.MouseButton1Click:Connect(ButtonClicked)
button.MouseButton1Click:Connect(ButtonClicked)

-- Prints "Button Clicked!" x3

1 Like

Yes. If you connect the same function to the same event multiple times the function will be ran however many times it was connected to the event.

Also because you’re still learning, when the event gives you arguments they’ll be automatically passed to the connected function

1 Like

Also another thing, so lets say a looped function or a connected one many times it will print something like:

-- "Printed statement" x7

So if I print it it will have a number saying however many times it was printed. Now if we print different strings multiple times what will the output be like?

For instance:

Print("A")
Print("B")
Print("C")
Print("D")

-- Pretend the prints above are in a function that ran twice

Would the output be:


-- "A" "B" "C" "D" x2 
-- or 
-- "A" "B" "C" "D" "A" "B" "C" "D"

It would print like:

A
B
C
D
A
B
.... 

This is because the print function runs whatever you’re printing on a new line

1 Like