Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I get the user who clicked a SurfaceGui?

I'm trying to get the name of a player when they click a SurfaceGui with a TextButton, but I have no idea how that would work. Any ideas?

4 answers

✓ Accepted answer

You have to add the SurfaceGui to their PlayerGui, and set its Adornee property, like so:

SurfaceGui.Adornee = Workspace.Brick

This will allow you to access the Player that has clicked the SurfaceGui, but any changes a Player makes to the SurfaceGui's contents (i.e. scrolling) will only appear for them.

3 comments

0

That's not what I had in mind... this is a tycoon...

0

Regardless, using the PlayerGui is the only way to tell who is using the GUI.

0

Ok.

Use what adark said.

Put the SurfaceGui in StarterGui, then set Adornee to brick. Then in a LocalScript in the Gui do this

local block = workspace.NAMEOFBLOCKHERE
script.Parent.Adornee = block
script.Parent.BUTTON.MouseButton1Down:connect(function()
print(game.Players.LocalPlayer.Name)
end)

gg no re

Use a LocalScript, perhaps in Backpack, or PlayerGui

local player = game.Players.LocalPlayer

local surfaceGui = player.PlayerGui.SurfaceGui

local block = Workspace.Block


surfaceGui.Adornee = block


surfaceGui.TextButton.MouseButton1Up:connect(function()

	-- Do Stuff --

end)

Ofcourse, this will only be visible to the local player.

If you wanted it visible to other player's, you'd have to make a system that updates the information on the surface gui, for each player. Or something along them lines.

There's not really any way that I know how to do this, but it is really easy using ClickDetectors which serve close to the same purpose. Assuming that your hierarchy would look something like this:

  • Part -- SurfaceGui --- TextButton/TextLabel -- ClickDetector --- Script

The script inside of the ClickDetector would look something like this:

script.Parent.MouseClick:connect(function(player)
	print(player.Name .. " pressed me!")
end)

1 comment

0

You can parent the SurfaceGui to the Player's PlayerGui (For example, by putting it in the StarterGui) to allow the scripts to know that that specific Player is using the SurfaceGui.

This discussion is preserved as it was. New answers, comments, and votes are closed.