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
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.
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.
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.
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)