Hello, is there a way to find the player that clicked a Text button in a Surface Gui from a script?
Yes, use a RemoteEvent
which will then tell the server what player was. Create a RemoteEvent
in ReplicatedStorage
and name it “ClickedButton” (for testing purposes), then create a Script
in ServerScriptService
, insert this code inside:
local replicatedStorage = game:GetService("ReplicatedStorage")
local remote = replicatedStorage.ClickedButton
remote.OnServerEvent:Connect(function(player, ...)
print(player.Name .. " clicked the button!")
end)
Then you can create a LocalScript
under the TextButton
and insert this code:
local replicatedStorage = game:GetService("ReplicatedStorage")
local remote = replicatedStorage.ClickedButton
local button = script.Parent
button.Activated:Connect(function()
remote:FireServer()
end)
Once you click the button (or multiple times) it will show it like MasonX890 clicked the button!
.
Some reference links to learn how this works:
- Remotes: RemoteEvent
- Button.Activated: GuiButton.Activated
It’s a server script inside a SurfaceGui instance so you wouldn’t be able to call FireServer()
.
I didn’t read correctly, sorry for my misunderstanding.
No problem, the thread’s poster could just use a local script inside a valid local script container, i.e; StarterPlayerScripts/StarterCharacterScripts and correctly reference the SurfaceGui’s GuiButton object from there instead.
There would be multiple buttons. They would be in different places. They would also be put into their place by a server script, Basically, when the player joins it makes a button for all of these shirts and parents it to a stand. Hopefully that makes sense
just get the LocalPlayer
and print(player.Name)
when it is Activated
.
It from a server script, they don’t work in the workspace, I don’t know why.
Send a RemoteEvent
(FireServer) and use a LocalScript
to recieve it.
That would work, but how do I get which player clicked? I need to prompt a purchase the clicking player, and only the clicking player.
Try getting the players Mouse.Hit.p
and check if your Part
with the SurfaceGui
is there.
Ok, this will work for a Text Button right?
Yes, but you need to make a separate part with the text button on it.
Also send this to a LocalScript through a Remote, otherwise this won’t get the mouse.
So, should i just do the part that the Surface Gui is on?
Say theres a part the SurfaceGui
is on. Make another part, weld it to the first, and put the TextButton
on it. But I really think a ClickDetector would be easier…
If i do i Image button would it work better?
ClickDetector
would be easier, just make a Part
with a clickdetector on it and don’t use any TextButtons
or ImageButtons
. That would be the easiest, but if you insist, Text and Image have no difference, the difference is the decorations.