Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I make the players nametag appear on mouse hover?

Asked by
iHavoc101 127
4 years ago

I want the players name tag to appear when I hover my mouse on the player. But I'm not sure how to start, can someone help?

This is what I got for objects, is there a way to tweak it for players?

local Player = game:GetService("Players").LocalPlayer -- Get the player
local Mouse = Player:GetMouse() -- Get the player's mouse

-- Just creating a name tag for this example. This is assuming the script is inside a ScreenGui that's in the PlayerGui.
local Tag = Instance.new("TextLabel",script.Parent)
Tag.Text = ""
Tag.Visible = false
Tag.TextColor3 = Color3.new(1,1,1)
Tag.Font = "ArialBold"
Tag.FontSize = "Size18"
Tag.BackgroundColor3 = Color3.new(0,0,0)
Tag.BackgroundTransparency = 0.5
Tag.TextXAlignment = "Left"

-- Connect the "Move" event to the mouse 
Mouse.Move:connect(function()
    local Target = Mouse.Target -- Get the object the mouse is hovering over
    if Target then -- Make sure the target exists

        -- Update the information when a target exists
        Tag.Text = Target.Name
        Tag.Size = UDim2.new(0,Tag.TextBounds.X,0,20) -- TextBounds.X will make the tag be as long as the text, giving it a more complimenting appearance
        Tag.Visible = true

    -- If no target exists, then...
    else

        -- Hide the tag
        Tag.Visible = false

    end 

    -- Update the tag's position with the mouse
    Tag.Position = UDim2.new(0,Mouse.X-Tag.AbsoluteSize.X,0,Mouse.Y-Tag.AbsoluteSize.Y)
end)

2 answers

Log in to vote
0
Answered by
IDKBlox 349 Moderation Voter
4 years ago

I actually wrote code for something like this a little while back. This is actually using a selection box. whenever it finds a humanoid within your mouse.Target, it'll parent that slectionbox to that character(Local only, so only the player who put their mouse over it will see it). There for if you connected this up with you 'Tag' gui, only youwill see their nametag pop up.

-- Get Service --
local RunService = game:GetService('RunService')
local Players = game:GetService('Players')

-- calling --
local player = Players.LocalPlayer
local mouse = player:GetMouse()

-- Creating --
local Selection = Instance.new('SelectionBox')
local Selected = nil

RunService.RenderStepped:Connect(function()
    local target = mouse.Target
    if target then
        local humanoid = target.Parent:FindFirstChild('Humanoid') or target.Parent.Parent:FindFirstChild('Humanoid')
        if humanoid then
            if Selection.Adornee ~= humanoid.Parent then
                Selected = humanoid.Parent
                Selection.Parent = humanoid.Parent
                Selection.Adornee = humanoid.Parent
            end
        else
            if Selection.Adornee ~= nil then
                Selected = nil
                Selection.Parent = nil
                Selection.Adornee = nil
            end
        end
    end
end)
0
Thanks, can u tell me if I should put the script in the workspace of not? iHavoc101 127 — 4y
0
Did you figure it out? if not No, LocalScript inside of StaterPlayer.StarterPlayerScripts IDKBlox 349 — 4y
Ad
Log in to vote
0
Answered by
Nowaha 459 Moderation Voter
4 years ago

You can check if the Target's parent contains a Humanoid, and if it does set the text to the Target's parent's name

0
thanks iHavoc101 127 — 4y
0
oh lol IDKBlox 349 — 4y

Answer this question