Identifying the Player who clicked a SurfaceGui In a TextButton

I’ve looked all through the Developer API page for TextButtons, but I’m unable to find any event that tells who clicked the TextButton.

Activated()
MouseButton1Click()
MouseButton1Down()
 InputBegan()

All of the above fails to return a player according to the devfourm, and instead returns click locations, the device type that clicked, or something else.

Is there something I’m missing? Or does Roblox seriously not have this as a feature yet?

2 Likes

The player would be the local player, because UI is client sided - it runs on the players device and not the server. If you need the player, you can get it with game.Players.LocalPlayer.

1 Like

Surface GUI’s are client sided? That’s pretty weird. I’ll try to pass over the LocalPlayer using a local script and a remote event when I get the time, and I’ll mark this if it works.

I think SurfaceGUI’s can be client or server sided? Depends on where you really put it I guess

1 Like

Surface GUI can be seen Server Side or in Client, if you used LocalScript.

1 Like

Did you meant that you need to detect the player who clicked the button?

1 Like

If it was really designed to be server sided I think roblox would have thought ahead and saved me from needing to spend more resources creating an entire remote event just to get a players name.

If the above suggestion works, then this will ruin the security of the script I was using, because it relys on getting the players username then using a server-side module script to see if they have perms to use the UI.

Last time I checked, Local Scripts and remote events can be spoofed. :frowning:

@iiheavenlymarsx Yes, I need to detect who clicked.

If a surface GUI is a descendant of the workspace, there is no way of getting which player clicked the button. An alternative could be click detectors or using GUI’s.

1 Like

Would @YummyLava57 's solution work? I hope so :sad:

game.Players.PlayerAdded:Connect(function(plr)

local surfacegui = script.Parent.Parent
local button = script.Parent

function mouse()

print( ...plr.Name... )
button.MouseButton1Click:Connect(mouse)

Don’t exactly know if this is gonna work but you can try.

2 Likes

RemoteEvents can help send signals from the client to the server.
RemoteEvents are very useful ex. Giving a player cash through
the Server instead of the Client (client basically doesn’t actually give player cash)

You can make a RemoteEvent and fire it from a LocalScript:

local RemoteEvent = -- Define
local TextButton = script.Parent
local Player = game.Players.LocalPlayer

script.Parent.MouseButton1Down:Connect(function()
RemoteEvent:FireServer(Player)
end)

Server Script:

local RemoteEvent = -- Define

RemoteEvent.OnServerEvent:Connect(function(Player)
print(Player.Name) 
-- Actions
end)
5 Likes

This seems to be a solution, just tested it and it works great!

You can design your Surface Gui in the workspace and script it locally, and once you’re done just set the Adornee of the Surface Gui to the part you want it to be on and move the Surface Gui to Starter Gui. This is what I do and it works very well and is probably the easiest way to do this.

3 Likes

If the problem is sending the username through a remote event, which would make it possible for exploiters to change the name and gain access, then you might be able to fix this by using the first parameter of a remote event. It will be set to the player/client who sent the remote.

From the api reference:

When firing a RemoteEvent from a client to the server, data can be included in the firing. By default, the functions connected to OnServerEvent will be passed the player who fired the event as the first parameter. If any other arguments are provided in the FireServer function, they will also be included in OnServerEvent after the player argument.

The code is slightly flawed, but the guy, you’re a genius, thank you =) What this guy wrote is useful for those who do part on the workspace and in the surface gui makes a button to find out who pressed it on server script