Button Script [ ASK ]

Is there any way to check when player click a button it will print the button name, because I want to pass the value to a function.

Or should I use UserInput???.. Chars

local button = PATH_TO_YOUR_BUTTON
button.MouseButton1Click:Connect(function()
    print(button.Name)
end)

If it’s a UI button:

local button ="Your_Button"
local Player = game.Players.LocalPlayer
button.MouseButton1Click:Connect(function()
   print(Player.Name.."has clicked on "..button.Name)
end)

If it’s an object button :

local Button = script.Parent -- or any location your button is at
Button.ClickDetector.MouseClick:Connect(function(Player)
  print(Player.Name.."has pressed on "..Button.Name)
end)

But how to make like I have couple buttons and if they click I pass the value to a string value with button name so if they click button with same name it will return end. But I want to make it with just one function rather than I have to make all same functions to every button, like this

and let’s say I have three buttons , I just need that one function, rather than making like this :

function button1Click()
	if open.Value == "Button1" then 
		return 
	end
end

function button2Click()
	if open.Value == "Button2" then 
		return 
	end
end

function button3Click()
	if open.Value == "Button3" then 
		return 
	end
end

so I want like something like this below, only one function needed.

function buttonClick(button)
	if button.Name == open.Value then
		return
	end
end

Button1.MouseButton1Click(buttonClick)
Button2.MouseButton1Click(buttonClick)
Button3.MouseButton1Click(buttonClick)

how to make something like this?

local buttons = script.Parent:GetChildren()

local function ButtonClick()
	--do stuff
end



for _,button in pairs(buttons) do
	if button:IsA("TextButton") then
		button.MouseButton1Click:Connect(ButtonClick)
	end
end

Yes, but how to check if button click has same name with string value, but the string value keep changing, so when we are let’s say on button 2 and we click button 1 it will change to button 1. So when we click Button 1, but the string value is Button 1 too, it will not do anything, but if we click Button 2, and the string value is Button 1, it will return something. Etc.

You can do that with a simple if statement my friend. If I understand correctly you can just set string to button 1 when button 1 is pressed and then when button 2 is pressed check if the value is equal to button 1 and go from there.

1 Like

Yes but I want to make with one function only rather than to make functions for every buttons.

I’m not sure I understand. From what I’ve gathered you want one function for two buttons and when you press the first it will change a string and when you press the second it will check the string… Is that right?