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
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?
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.
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.
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.
Hopefully those help solve your issue!