Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to make it so GUI only appears when mouse is hovering over Door?

So this is my GUI client for my door. I am wondering how I would edit it so that another condition is added that asks if the mouse is hovering over the door. Code:

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")


local Door = game.Workspace.ButtonDoor.PrimeDoor
local DoorPart = Door.Door1
local DoorPrimaryPart = Door.PrimaryPart

local event = game.ReplicatedStorage.ToggleDoor

local NearDoor = false

RunService.RenderStepped:Connect(function()
	if Player:DistanceFromCharacter(DoorPrimaryPart.Position) < 5 then
		NearDoor = true
		DoorPrimaryPart.EToOpen.Enabled = true
		DoorPrimaryPart.EToOpen2.Enabled = true
	else
		NearDoor = false
		DoorPrimaryPart.EToOpen.Enabled = false
		DoorPrimaryPart.EToOpen2.Enabled = false
	end
end)

UserInputService.InputBegan:Connect(function(Input)
	if Input.UserInputType == 	Enum.UserInputType.Keyboard then
		local Key = Input.KeyCode
		if Key == Enum.KeyCode.E and NearDoor == true then
			event:FireServer()
		end
	end
end)

1 answer

There are two ways I thought of that you could try. You can check if the mouse's hit is near the door or you can check if the target is the door.

Hit: Use a magnitude check to check the distance between the mouse and the door:

local distance = 7

if (door.Position - mouse.hit.p).magnitude <= 7 then
	-- run code
end

Pros: More accurate than Target Cons: GUI may not show up when you want it to because distance is too low or high (easily fixable though)

Target: Use a simple if statement to see if the mouse's target is the door

if mouse.Target == door then
	-- run code
end

Pros: Easy to write and saves time Cons: It is buggy and not always precise

Link to mouse docs: Mouse Docs

1 comment

0

For the most part it works, I need to hover over the door to see the GUI, but when I then put the mouse off the door, it still shows the GUI.

This discussion is preserved as it was. New answers, comments, and votes are closed.