Player name appear on GUI once clicked

Hello developers, I am trying to make a script that when you click a player their name appears on a gui, this is the script I’ve got so far.
Here is the script I’ve got so far.

local clic = Instance.new("ClickDetector")
clic.Parent=script.Parent

function onClicked()
	local name = clic.Parent
	print(name.Name)
	local char = script.Parent
	local plr = game.Players:FindFirstChild(char.Name)
	if plr then
		print("found player")
		local gui = plr:FindFirstChild("PlayerGui")
		if gui then
			print("found the ScreenGui")
			local text = gui:FindFirstChild("PlayerINFO")
			if text then
				text.Frame.Input.Text=clic.Parent.Name
			end
			
		end
	end

	
end
clic.MouseClick:Connect(onClicked)

Here is my workspace
image

1 Like

Is this supposed to be when you click yourself?? Because thats what this script is doing. If you want to find the player that clicked the clickDetector then you need to add a a plr parameter to the onclicked function. like this:

onClicked(plr)

the plr variable will be equal to the player that clicked the click detecotr so you can display it on their screen.

3 Likes

ClickDetectors are only for 3d objects in the 3d workspace, you will have to use a GUI Button instead.

Here’s a simple sample script:

local TextButton1 = script.Parent

TextButton1.Activated:Connect(function()
    TextButton1.Text = game.Players.LocalPlayers.Name
end
1 Like

And if you want it to work with a serverscript. (I don’t think you would do this with a player gui, altho.)

local TextButton1 = script.Parent

TextButton1.Activated:Connect(function(plr)  --Parameter to get the player who clicked the button.
    TextButton1.Text = plr.Name
end

They are already putting a click Detector into the Character. The script is a child of character when the character is added. Although I’m not sure if clickdetectors work on whole models. EDIT: They do work on whole models.

Alright, I will try all those ideas!

Oh, I misread the title, I thought he wanted the player name to appear on gui, once the gui is clicked.

1 Like

The name appears on the GUI once the player is clicked sorry about the confusion.

This is all you have to do considering the GUI is already in its proper place.

local clic = Instance.new("ClickDetector")
clic.Parent=script.Parent

function onClicked(plr)
	local name = clic.Parent.Name
	if plr then
		local gui = plr:FindFirstChild("PlayerGui")
		if gui then
			local text = gui:FindFirstChild("PlayerINFO")
			if text then
				text.Frame.Input.Text= name
			end
		end
	end
end
clic.MouseClick:Connect(onClicked)
1 Like

How could I send the player name to a different script? As I am trying to make a GUI that whn you click a player their name appears on a gui, Which we’ve done. After that I am trying to make it so that you press a button on the GUI and then it gives the selected player admin.

You can send the players name to a different script with a BindableEvent. It is like a remoteEvent but it is for local communication. Whether it is for two server scripts to communicate to communicate to eachother, or two local scripts to communicate with eachother. If you want to send data from a client to a server and vice versa you would use a remote event.

Thank you very much I will try this!