Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

How do I make a GUI pop up if a player hovers over a certain part?

Asked by 4 months ago

I'm trying to make a door script where if a player hovers over the door handle, a GUI pops up that prompts them to open the door. I currently need help figuring out how to make the GUI pop up when the player hovers over the door handle.

Here's all I could come up with:

local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
local handle = game.Workspace.Handle

function enter()
    if mouse.Target.handle == true then
        game.StarterGui.OpenDoor.Open.Visible = true
    else
        game.StarterGui.OpenDoor.Open.Visible = false
    end
end

2 answers

Log in to vote
0
Answered by
xPolarium 1337 Moderation Voter
4 months ago
Edited 4 months ago

StarterGui is a folder which copies it's children to a player's PlayerGui each time their character spawns. The correct Gui you should be editing should be in the player's PlayerGui.

The paths for lines 07 and 09 should be:

--If the ScreenGui in PlayerGui is called "OpenDoor":
plr.PlayerGui.OpenDoor.Open.Visible = true

You could also define PlayerGui outside of this function to prevent having to rewrite the entire path again multiple times.

The other thing I see is how you check if mouse.Target is whatever handle is. I believe the correct set up should be like this:

local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()

local handle = workspace.Handle

function enter()
    if mouse.Target == handle then
        plr.PlayerGui.OpenDoor.Open.Visible = true
    else
        plr.PlayerGui.OpenDoor.Open.Visible = false
    end
end

--how is enter() called btw?

Know that mouse.Target gives you a BasePart, so make sure what you compare to is actually a BasePart. See this documentation on this.

If I missed something or you need something explained then let me know.

Edit:

You could use a loop to check if the Target is the part you want. I use a while loop here:

while true do
    if mouse.Target == handle then
        OpenGui()
    else
        CloseGui()
    end
    wait()
end

Mind you, the above is only an example and should contain a way to break this infinite loop. Other alternatives you could look into is the MouseHoverEnter from a ClickDetector.

0
Script still doesn't seem to work. https://gyazo.com/29ae63edfad399178c91a95562c01adf 1swenzje 27 — 4mo
0
Btw, this is the script I used. I changed it a little to make it look more organized. https://gyazo.com/e4efdf9d1a4a9f5aca21b9043cac4c08 1swenzje 27 — 4mo
0
Updated post to include an example of what you can do with the code you're using above this comment. Since I overlooked the hover effect in your OP, look into the MouseHoverEnter event of a ClickDetector. x xPolarium 1337 — 4mo
Ad
Log in to vote
0
Answered by
Isaque232 171
4 months ago
Edited 4 months ago

As xPolarium said you'd need to edit PlayerGui instead of the StarterGui but he already explained it.

Seeing your script currently ( Script ) I've noticed that there's no event that triggers the script, so I'm going to point out some ways you could change your script.

When the mouse moves it executes the script

mouse.Move:Connect(function()
    if mouse.Target == handle then
        OpenGui()
    else
        CloseGui()
    end
end)

When you click it executes the script

mouse.Button1Down:Connect(function()
    if mouse.Target == handle then
        OpenGui()
    else
        CloseGui()
    end
end)

You can also use events from ClickDetector such as

MouseHoverEnter - Fires when the player’s mouse begins hovering over the ClickDetector's parent.

and

MouseClick, RightMouseClick - Fires when the player clicks | right clicks while hovering with It's cursor through the part that has the ClickDetector.

Hopefully those help solve your issue!

Answer this question