How to mouse target = player show player name?

Hello, I thought about making: if mouse enters a player then it will show the name of the player, as you know I’m a pretty starter developer, I got trust level Member like few days ago and I’m still like half new in developing. Maybe someone could help me make the mouse entered on player show player name

Thanks!

4 Likes

Doesn’t anyone think the title is weird? Just me?

Here are some resources which will enable you to code the desired function yourself.

Mouse.Target
RunService.Heartbeat

You can try it yourself first and if have any questions you can make a post here I will try to help out.

1 Like
1 Like

Where do I place the local script?

Why do I need Heartbeat? How will Heartbeat help me?

Forummer’s Solution is a better solution.

But I still don’t know where to place the local script he said :frowning: :slightly_frowning_face:

StarterCharacterScripts container.

Oh ok I’ll try now

For 30 letters sorry, just ignore this

Will it work on a dummy?

Sorry again

No, it’s designed for the local player’s character.

local players = game:GetService("Players")
local player = players.LocalPlayer
local mouse = player:GetMouse()

mouse.Move:Connect(function()
	local targetPart = mouse.Target
	if targetPart then
		local targetModel = targetPart:FindFirstAncestorOfClass("Model")
		if targetModel then
			local targetHuman = targetModel:FindFirstChildOfClass("Humanoid")
			if targetHuman then
				print(targetModel.Name)
			end
		end
	end
end)

Here’s a script which will work on dummies, it prints the dummy’s name to the console.

1 Like

It says humanoid and not the dummy name (model)

image

I forgot your intention was to print the NPC’s name.

Replace print(targetHuman.Name) with print(targetModel.Name) instead.

Oh ok, but so I wanted a gui appear when the mouse is on a player, and change the text to the name of the player

But still thanks!

Well you’d need to design your own UI for that. But the same logic applies, you’d just set a TextLabel’s text to the name of the player/NPC hovered over.

I did just now, but it didn’t really work

It was only once if my mouse hover on dummy it appeared but then if my mouse goes of it still there

local players = game:GetService("Players")
local player = players.LocalPlayer
local mouse = player:GetMouse()

mouse.Move:Connect(function()
	local targetPart = mouse.Target
	if targetPart then
		local targetModel = targetPart:FindFirstAncestorOfClass("Model")
		if targetModel then
			local targetHuman = targetModel:FindFirstChildOfClass("Humanoid")
			if targetHuman then
				print(targetModel.Name)
			else
				--Do code.
			end
		else
			--Do code.
		end
	else
		--Do code.
	end
end)

Where I’ve commented Do code. you should replace with code which wipes the text clean.

1 Like

This script will display the name of a character model, if it is a regular character model it will only display the name of the model, but if it is a player’s character it will display their DisplayName and their UserName. In the Explorer I have it so that the code below is in a LocalScript, The LocalScript is in a TextLabel, the TextLabel is in a ScreenGui, and the ScreenGui is in StarterGui.

I’ve left notes explaining the purpose of each part of the code.

local USERINPUTSERVICE = game:GetService("UserInputService") -- In this case, this service will be used to hide the player's cursor
local PLAYERS = game:GetService("Players") -- In this case, this service will help get this player and validate wheather a character model is a real player

local UI = script.Parent -- the textlabel
local MOUSE = PLAYERS.LocalPlayer:GetMouse() -- Gets the player's mouse
local CAMERA = workspace.CurrentCamera -- the player's camera

MOUSE.Move:Connect(function() -- Runs the code below when player's mouse moves
	local CAMERAPOSITION = CAMERA.CFrame.Position -- the camera position
	local DIRECTION = CFrame.new(CAMERAPOSITION, MOUSE.Hit.Position) -- gets the direction of where the mouse is pointing to from the camera (for raycasting)
	local HITOBJECT = workspace:Raycast(CAMERAPOSITION,DIRECTION.LookVector*300) -- casts a ray to check for objects the mouse is pointing at

	if HITOBJECT then -- Checks to makes sure the mouse is pointing at somthing and not the sky/void

		local PATH = HITOBJECT.Instance.Parent -- Line 15 - 21 Checks that the player is hovering over a character by looking for a humanoid
		if not PATH:FindFirstChildOfClass("Humanoid") then
			PATH = PATH.Parent
			if not PATH:FindFirstChildOfClass("Humanoid") then
				PATH = nil
			end
		end

		if PATH then -- Checks to see if a character was found
			local PLAYER = PLAYERS:GetPlayerFromCharacter(PATH) -- Gets the player controlling the character
			if PLAYER then -- Checks to make sure that the character isn't an NPC or somthing else and is an actual player
				UI.Visible = true -- shows the txtlabel
				UI.Text = PLAYER.DisplayName.." - @"..PLAYER.Name -- puts the player real name and display name on the textlabel
				UI.Position = UDim2.new(0,MOUSE.X,0,MOUSE.Y) -- moves the ui's position to the mouse's position
				USERINPUTSERVICE.MouseIconEnabled = false -- hides the player's cursor
			else -- what to do if the mouse isn't hovering over a player's character but it is still a character
				UI.Visible = true -- shows the txtlabel
				UI.Text = PATH.Name -- puts the player real name and display name on the textlabel
				UI.Position = UDim2.new(0,MOUSE.X,0,MOUSE.Y) -- moves the ui's position to the mouse's position
				USERINPUTSERVICE.MouseIconEnabled = false -- hides the player's cursor
			end
		else
			UI.Visible = false -- hides the textlabel
			USERINPUTSERVICE.MouseIconEnabled = true -- shows the cursor
		end
	else -- what to do if the ray casted found nothing
		UI.Visible = false -- hides the textlabel
		USERINPUTSERVICE.MouseIconEnabled = true -- shows the cursor
	end
end)
2 Likes