Click a player followed by a name pop up in a GUI

Hi, I am creating this kind of security checkup system. Where it would show the inventory, name, group rank and some other information. But I cannot figure out how I can detect a player is being clicked. So I have the gui in StarterGUI. Where would I put the script that detects I clicked on a player? And would it be possible to have like a box around a player (locally) when I hover that player?

You can use a raycast from the localplayers character to the mouse position - when the mouse is clicked you can check if the raycast result is a players character and then you can just go on about adding the gui and inserting a “SelectionBox” to the player (also make sure to adornee the SelectionBox to the player’s character)

1 Like

This might be a starting point for you (the localscript is in StarterPlayer and is just a demo and not your finished product)

local Players 		= game:GetService("Players")
--
local localplayer 	= Players.LocalPlayer
local mouse 		= localplayer:GetMouse()
--
local maxDistance = 1000 --studs
--
mouse.Button1Down:Connect(function()
	local character = localplayer.Character
	local vector = mouse.Hit.LookVector
	
	local raycastParams = RaycastParams.new()
	raycastParams.FilterType = Enum.RaycastFilterType.Exclude
	raycastParams.FilterDescendantsInstances = {character}
	local raycastResult = workspace:Raycast(character,vector*maxDistance,raycastParams)
	
	if raycastResult and raycastResult.Instance then
		local targetCharacter = raycastResult.Instance.Parent
		if not targetCharacter:FindFirstChild("Humanoid") then
			return 
		end
		
		local targetPlayer = Players:GetPlayerFromCharacter(targetCharacter)
		local SelectionBox = Instance.new("SelectionBox",targetCharacter)
		SelectionBox.Adornee = targetCharacter
		
		--execute other gui code
	end
end)

Hope this helps!

1 Like