How to check which player clicked on a SURFACE GUI TEXT BUTTON

Why do you need to destroy your buttons and put them back every 10 seconds?

1 Like

Well, you can pass the player from local to server side with a RemoteEvent.
So on the client side you check for the MouseButton1Click and then fire the remote; on the server you just listen to OnClientEvent and do what you need there.

In this case you can listen to the ChildAdded event for the SurfaceGui and from there you just listen to the MouseButton1ClickEvent and as I wrote above, use a RemoteEvent.

1 Like

Think of this like a pet system, but in a surface gui. You’d need to delete all of the pet gui elements to periodically refresh the pet system gui.

Why would you need to delete the UI elements? You could just update them with the latest information.

thats just the easiest way implement it

No, it isn’t. You can’t expect help if you’re not going to put forth your best effort.

A text button will ALWAYS be client-side.

1 Like

When it’s parented to the StarterGui folder inside of a ScreenGui instance, yes but in this instance it’s inside a SurfaceGui instance shared by the server.

You can just Parent the ‘SurfaceGui’ to the StarterGui and set its ‘Adornee’ property to a part. Use ‘LocalScript’ also so you can define the player by ‘game.Players.LocalPlayer’

local Players = game:GetService("Players")
local Player = Players.LocalPlayer

local Part = workspace.Part
local Gui = Part.SurfaceGui
local Button = Gui.TextButton

Button.MouseButton1Click:Connect(function()
	print(Player.Name)
end)

image

repro.rbxm (5.8 KB)

No hacky solutions necessary, just have a local script (placed inside StarterPlayerScripts) handle the button’s behavior.

All of the TextButton’s events that are inherited from GuiButton and GuiObject will only fire on the client. So as I said, it’ll ALWAYS be client-side regardless of its parent.

https://developer.roblox.com/en-us/api-reference/class/TextButton

I was under the impression that you were inferring that the button’s existence itself was local.

A text button will ALWAYS be client-side.

All of the TextButton’s events that are inherited from GuiButton and GuiObject will only fire on the client.

This is only correct if you listen for the event to fire on the client-side (via a local script), the RBXScriptSignal objects of GuiButton instances will fire on the server (if listened for via server script).

There’s no other way than to do it on the client side and communicate with the server via a remote event or function

You can put SurfaceGui to the StarterGui and change the SurfaceGui Adornee to the part. So now you can check if a player clicked on a textbutton and get the player who clicked it.

1 Like