Click a Player then their name appears on a GUI

Hey developers, I am very new to scripting, and I want to be able to create a system listed below and be able to understand how it works.

  1. What do you want to achieve? I want to be able to have a system where if a player has a certain GUI open, then clicks another player then the player that has just been clicked name appears on a GUI.

  2. What is the issue? I can’t find any way to do this, I’ve searched youtube, google and the dev forums.

  3. What solutions have you tried so far? I haven’t managed to try anything as I don’t know what is required to make this feature happen.

Here is my workspace if you need it. image
The text label is part of the GUI is where the Players name will appear.

1 Like

You can add a Script in StarterPlayer > StarterCharacterScripts with:

local clic_detector = instance.new("ClickDetector")
clic_detecor.Parent = script.Parent.Torso

– then when someone clicks the player, you need to add a function that will get the Player Name. You can search up Get Player name with clickdetector on the DevForum or google,

Hope this helped at least abit!

EDIT: ill make the scripts and give you the model

1 Like

Ok, thank you very much, it’s very appreciated, and I thank you so so much! :grinning:

2 Likes

Get the mouse.Target and find the characters name. Then find the players name in the playerlist to confirm that it is an actual player and not a NPC.

local Mouse = game.Players.LocalPlayer:GetMouse()

function MB1()
     local Target = Mouse.Target
     local Checking = true
     local HighestAncestor = Target
     while Checking == true do
         if HighestAncestor == game.Workspace then
              Checking = false
              break
         else
              HighestAncestor = HighestAncestor.Parent
         end
         if HighestAncestor:IsA("Model") then
              local Players = game.Players:GetChildren()
              if Players[HighestAncestor.Name] == nil then
              -- It Could be a NPC
              else
              -- Create The Gui
              end
         end
     end
end
Mouse.Button1Down:Connect(function(MB1()))

-- Disconnect this when the player leaves.

If this game is for PC then use the mouse.Target

If it needs more compatability then use Userinput service

You may have noticed I added in the start of npc detection in there, you can develop on that if you want to have NPC trade windows or interactions using GUIs

2 Likes

As promised, here is the model

  • Place the Info Script in StarterCharacter
  • Place the Gui in StarterGui

Edit what you wish PlayerInfo.rbxm (4.6 KB)

Dont forget to mask Solution if it worked

2 Likes

Thank you ever so much, I can’t thank you enough!

2 Likes

Hey, this is the script that I was given,

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("StaffMenu")
		if gui then
			print("found the ScreenGui")
			local text = gui:FindFirstChild("Role_Assignment")
			if text then
				text.Frame.Input.Text = clic.Parent.Name
			end	
		end
	end
end
clic.MouseClick:Connect(onClicked)

And there’s nothing in the output, so there could be a problem really early on in the script, here is my explorer if you need it.image

1 Like

I recommend using GetPlayerFromCharacter on the local ‘plr’ instead of using that method.

local plr = game.Players:GetPlayerFromCharacter(char)

And that script is trying to access the GUI directly through the Player object, but the GUI is inside PlayerGui, so you’d need to change it!

local gui = plr.PlayerGui:FindFirstChild("StaffMenu")
3 Likes

Ok thank you, I will try that.

1 Like

Ok, it does work, however it doesnt put the player’s username on the gui, it puts it on the Dev console (Output).

1 Like