Hello there, I was wondering how to make a GUI that appears when a mouse hovers over an object (just like it says in the title).
Hey there!
You could use the MouseEnter event on a UI object.
Note that this event doesn’t seem to be connected when the UI object’s visible property is false, so as a workaround I used a blank UI with BackgroundTransparency
set to 1 as a “hitbox” for the other elements.
Sample code (anything in the “Children” folder will be visible/invisible when the mouse enters and leaves):
script.Parent.MouseEnter:Connect(function()
for _, Child in ipairs(script.Parent.Children:GetChildren()) do
Child.Visible = true
end
end)
script.Parent.MouseLeave:Connect(function()
for _, Child in ipairs(script.Parent.Children:GetChildren()) do
Child.Visible = false
end
end)
What does ipairs do?
characters ignore
ipairs
goes through every object in a table. In this case, I have all the frames and other UI stuff inside a folder
. The :GetChildren()
function gives me a table of all the UI stuff in the folder, and ipairs
goes through them and does something (in this case, makes them visible or invisible).
Whats the difference between ipairs and normal pairs?
You can do
local UIS = game:GetService(“UserInputService”)
local pickupKey = “B”
local Player = game.Players.LocalPlayer
local mouse = player:GetMouse()
UIS.InputBegan:Connect(function(input)
if mouse.Target then
if mouse.Target:FindFirstChild(“VALUENAMEINSIDEPART”) then
– Do script
end)
If you need more explaining on what this does I’m happy to explain
Whats mouse.Target???
It detects what the mouse is hovering over
ipairs
goes through them in numerical order, and stops if something in the table doesn’t have a number index. pairs
keeps going until something is nil, and doesn’t care about order.
So for example
myTable = {1, 2, 3, 4, 5}
for Index, Item in ipairs(myTable) do
print(Item)
end
Could be indexed and properly work using ipairs
, because there are no “holes” or non-number indexes, but something like
myDictionary = { ["Gold"] = 200, ["Diamonds"] = 500, ["Player"] = game.Players.LocalPlayer}
for Index, Item in ipairs(myDictionary) do
print(Item)
end
would not work because the indexes are words, not numbers (Gold, Diamonds, Player). If you don’t set the index normally, they are numbers, and start from 1.
Insert or instance a ClickDetector onto the part. Now, make your GUI, make sure everything is parented onto a frame so that it’s easier to code. I’ll be explaining every part of the way.
First, we need to get the script to know where the part is, and the click detector. This can be achieved by using variables. Make sure this is a local script in your gui
local Part = workspace. --YOUR PART HERE
local ClickDetector = Part.ClickDetector
local Frame = script.Parent
Make sure to replace the Your Part Here with a reference to your part. Now, let’s get our events in, which gives a signal to a function so that it runs. Let’s get to know our functions we are going to be using
MouseHoverEnter is fired when the mouse hovers over the object.
And if you want to fire something when the part is clicked, use MouseClick
ClickDetector.MouseHoverEnter:Connect(function()
end)
In those two opening and closing functions, we need to add something so that it well does something. In this case, making a Gui open up.
Frame.Visible = true
…will make the Frame visible with your contents.
Woo, we made something pop up! Hope this helps.