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

Print Player Name When Clicked on GUI Button?

Just trying to get a player's name when they click on a GUI Button. Thanks!

2 answers

✓ Accepted answer

To get a players name when they click on the GUI or whatever it is you will need to put a local script into the button and insert this script. You must use

local player = game.Players.LocalPlayer
local Button = "" -- here your going to remove speech marks and type the path to the button e.g.
--if you have the script inside of button you would do script.Parent

function OnClicked() 
print(player.Name.." Clicked me and it hurt...") -- if you want to just get the name delete the clicked --me and it hurt part including speech marks DON'T remove player.Name 

end

Button.MouseButton1Down:connect(OnClicked)   -- you can change MouseButton1Click to --MouseButton1Down they're basically the same

and heres it without extra parts or comments

local player = game.Players.LocalPlayer
local Button = 

function OnClicked()
  print(player.Name)
end

Button.MouseButton1Click:connect(OnClicked)

so you will know what to do next time i'm going to explain it to you now

first we're making some variables I assume you know what they are after that were going to make the function I've called it OnClicked but you can call it whatever inside that function we are going to use a basic print() function and inside were going to put player.Name player is the variable which is going to go to variable and scan through the online players to find the player that clicked and print there name. then were going to end the function and use a connector which will be either MouseButton1Down (recomended) or MouseButton1Click and connect it to OnClicked if you want it to be the right mouse button use MouseButton2 instead of 1 remember in the variable "Button" to get rif of the speech marks and put the path to the button if the script is inside the button do script.Parent

make sure it is in a local script

"You can get the player using the LocalPlayer property of Players service from a LocalScript. If you weren't using a LocalScript, you should consider doing so for tasks involving or relying on any sort of input from the client or (keyboard, mouse, camera manipulation..)." - quoted from here

local player = game.Players.LocalPlayer
local button = "path to TextButton"

function onClicked()
	print(player.Name)
end

button.MouseButton1Click:connect(onClicked)

1 comment

0

sup

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