How to detect when Surface GUI Text Buttons have been clicked?

The title says it all. I want to know how I can detect when a SurfaceGui Text Button has been clicked, and by who.

But you can just use a click detector on a part.

That’s not the goal, since I want to have multiple buttons on one part.

1 Like

Ok maybe you can try to use the mousebutton1up even on the text button?

I’ve tried that, and it has not worked.

do

TextButton.MouseButton1Click:Connect(function()

function clicked()
–script here

script.Parent.MouseButton1Click:Connect(clicked)

They tried that, but it won’t work.

Unfortunately the TextButton does not return the player (it should, right?)
So it only remains to use RemoteEvents.
Put the gui in StarterGui and set Adornee where it was, then change the scripts to localscript and connect it to the RemoteEvent.

I might’ve found a solution, but I’m not sure.

So what you’d do is that you would add a surface gui into starter gui, then inside the surface gui, add a textbutton. Next, adornee the surface gui to the part. Last, make a local script inside the surface gui and type this:

local textButton = script.Parent.TextButton
local player = game:GetService("Players").LocalPlayer


textButton.MouseButton1Up:Connect(function()

    print("Button has been clicked by: " .. player.Name)

end)

Add this in the SurfaceGUI


local Button = script.Parent.TextButton  --Define the location of your button
local player = game:GetService("Players").LocalPlayer


Button.MouseButton1Up:Connect(function()  --function the button
--Add what you want to function here. (Ex. Make a part change color when clicked)
    print("The Button is clicked") --This will print in the output

end)

NGL, I’m not sure how this isn’t solved yet. Just use a server sided script and you get the player when they join using

local Players = game:GetService("Players")
Players.PlayerAdded(function(player)
--YOUR CODE
end)

Did you accidentally put this in the uh wrong thread? This one’s asking about how to detect SurfaceGUI button clicks but also to detect who clicked it. So I guess you technically answered it but only partly.

Anyways since the topic has already been bumped, I might as well solve it for anyone else that stumbles across this thread:

Now to have SufraceGUI button functionality, you do need to adorn your GUI element to the SurfaceGUI. Here’s what I mean:

First, create your TextButton in a ScreenGui in STARTERGUInot in the SURFACEGUI on your part!

Next of all, go to your SurfaceGui’s properties and look for the

Adornee - SurfaceGui Property.

Then, click on the empty box next to the Adornee text. After that, you just wanna find the part you need to adorn to and click it (in the Explorer).

Nice. Now, all we have to do is slap a LocalScript into the TextButton and we should be good to go!

-- example code
local Btn = script.Parent
local Player = game.Players.LocalPlayer -- since this is a LocalScript
Btn.MouseButton1Click:Connect(function()
   print("Success!")
   print(`{Player.Name} clicked THIS button!`)
end)

Hopefully this answers your questions :slight_smile:

OP hasn’t been active in a while so I don’t know if this will be marked as a solution anytime soon (it should work though!)

No, I said it cause I had trouble registering a SurfaceGui click using a localscript so I just decided to use a Server Script which just fixed it.