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:
If anyone can help me then that would be great. Thanks!
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);
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
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
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.