How to make text show up when hovering over something

I’m not the best at coding but I was trying to make it so that whenever you hover over a object it would display text.

The intended function

This is the code I used

local object = script.Parent 

local textLabel = Instance.new("TextLabel")
textLabel.Size = UDim2.new(0, 200, 0, 50)
textLabel.Position = UDim2.new(0, 0, 0, 0)
textLabel.BackgroundTransparency = 0.5
textLabel.BackgroundColor3 = Color3.new(0, 0, 0)
textLabel.TextColor3 = Color3.new(1, 1, 1)
textLabel.Text = "Hover Text" -- the text
textLabel.Visible = false
textLabel.Parent = game.Players.LocalPlayer.PlayerGui 

object.MouseEnter:Connect(function()
    textLabel.Visible = true
end)

object.MouseLeave:Connect(function()
    textLabel.Visible = false
end)

I have this in a localscript, which is parented to the cube. the cube is just named “Part” if that matters.

It doesnt work and I’m at a standpoint, any solutions? :3

3 Likes

The textlabel needs to be under a screengui in playergui to be visible.

1 Like

Here there is no mouse path, so you would need to add the player’s mouse:

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

local mouse = player:GetMouse()

Then there would have to be some kind of a button that would be on the object. I am not entirely sure about this kind of thing, I am working on something like this too, I think using this with a ScreenGui would be easier but then that wouldn’t be in the 3D space.