Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Print Player Name When Clicked on GUI Button?

Asked by 8 years ago

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

2 answers

Log in to vote
0
Answered by 8 years ago

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

Ad
Log in to vote
0
Answered by
ImageLabel 1541 Moderation Voter
8 years ago

"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)
0
sup turtle2004 167 — 7y

Answer this question