Mouse GUI while hovering over a player or part

I don’t know what code to use for scripting a GUI interface that displays whenever a player’s mouse hovers over another player, a Humanoid, or a certain part.

Here’s a screenshot of the GUI that I will modify later:
Screenshot_3

If anyone can help me then that would be great. Thanks!

6 Likes

The easy way of doing it is just register what the Player’s mouse.Target value is and setting the text to be the name.

I just hopped into studio real fast and made a quick little version of it that checks if the Mouse.Target is a Player, Model, or an Object. You don’t need to have it run on RenderStep, I just did that with mine. Mine also creates the GUI for you, whereas in your case you already have the GUI made, so you can just reference it and change the text. This is just a little full blown demo.

If all you’re looking for is the code to grab what the Player’s mouse is hitting, you only need the first 2 lines, and from “local Target…” to the end;

local Player = game:GetService("Players").LocalPlayer;
local Mouse = Player:GetMouse();
local UIS = game:GetService("UserInputService");
local RS = game:GetService("RunService");

local Gui = Instance.new("ScreenGui", Player:WaitForChild("PlayerGui"));
local Label = Instance.new("TextLabel", Gui);
Label.Size = UDim2.new(0, 100, 0, 10);
Label.AnchorPoint = Vector2.new(0.5, 0.5);

RS:BindToRenderStep("MouseHover", Enum.RenderPriority.Camera.Value - 1, function()
	
	local mPos = UIS:GetMouseLocation();
	Label.Position = UDim2.new(0, mPos.X, 0, mPos.Y);
	
	local Target = Mouse.Target;
	
	if Target then
		if Target.Parent then
			if Target.Parent:IsA("Model") and not (Target.Parent == workspace) then
				local tPlayer = game:GetService("Players"):GetPlayerFromCharacter(Target.Parent);
				if tPlayer then
					Label.Text = tPlayer.Name;
				else
					Label.Text = "Model: " .. Target.Parent.Name;
				end;
			else
				Label.Text = "Object: " .. Target.Name;
			end;
		else
			Label.Text = "Object: " .. Target.Name;
		end;
	else
		Label.Text = "nil";
	end;
	
end);
8 Likes

I did figure out a simpler system to use, but I’ll run through with that script. By the way, is there any line of code I can use to limit a player’s distance from the GUI trigger? For example, if the player is too far away, then he or she won’t see the GUI despite hovering their mouse over the trigger.

Here’s my LocalScript(s):

First one, placed as a direct child of the ScreenGui:

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()

while wait() do
	if Mouse.Target ~=nil and Mouse.Target.Name == 'JournalBrick' then
		script.Parent.RuffianIIJournal.Visible = true
	else
		script.Parent.RuffianIIJournal.Visible = false
	end
end

Second LocalScript placed in the Frame:

local player = game.Players.LocalPlayer

local mouse = player:GetMouse()

while true do

wait(.05)

script.Parent.Position = UDim2.new(0, ((mouse.X)+15), 0, ((mouse.Y)+20))

end
2 Likes

Use an if statement with this. Vector3 | Documentation - Roblox Creator Hub

You don’t need two loops here. Simply set the position in your first loop when you’ve verified the mouse is over the part; you don’t need to keep changing it when it’s not visible.

To check the players distance you can do a simple Magnitude check.

if (Character.Head.Position - Part.Position).Magnitude <= MaxDistanceInStuds then
1 Like

Use the mouse.X and mouse.Y values to position the user interface, use magnitude to check if the player is within the region of the part.

Sorry for bringing this up again… just curious… what would be a “hard way” and would it have any benefits over this? I get that the mouse object isn’t the best thing out there, but there still are things that can’t be done with User Input Service. I made my own “Mouse” with it, but it sure isn’t as convenient.

For some ungodly unknown reason, I’ve seen people go and check every Player in the game and get the names and just have a UI full of the text labels and then they hide all of them except the one that the player is hovering over.

If it’s client-based, it can be regulated by this:

local Character = game.Players.LocalPlayer:WaitForChild("Character")
local Part = game.Workspace:WaitForChild("Part")
local max = 5 -- units (studs)
if (Character:FindFirstChild("HumanoidRootPart").Position - Part.Position).Magnitude <= max then
   -- Code
end

I’d actually use CollectionService for this since you’d probably be using more than one. You could implement a(n) Heartbeat with RunService that creates and destroys the label based on proximity. Personally, I’d create the element before-hand and set it’s visible property based on proximity (close = true, far = false). If you’d like to add spice to it, you could use TweenService.

2 Likes

You know that was an example of the hard way that I’ve seen people do it, right? That’s not how I do it nor, how I ever would.

And the code I sent as a first reply to this back in 2018 was code I had used back in 2016 and didn’t really change at all, so it was old code af.

I just use a TextLabel and change the text of it.

I will answer this when I am home, are you still requiring the code?

Read the first response to this thread after it was created.

Also this thread is from 2018, so whatever they were trying to do then, is fully figured out by now.

Hey I’m new here so I’m still learning how things work haha

All good haha, hope you’re enjoying the dev forum!

3 Likes