How would I implement Mouse.Target or Raycast into my door script?

Hello!
I’ve been working on this door and can’t find anything that works for what I need.
I’ve search for ATLEAST 5-6 hours on raycasting and Mouse.Target but can’t find any resources that work.
Worked around 10 hours total (3 days time) trying to code the single part.
I’m basically trying to make something like this:


This is my go at it:

There’s already a ton of issues with my door, and it doesn’t include the part when the mouse is over the door.
Issue 1. Door seems to not open correctly on the backside?
“Issue” 2. I can’t figure out how to add a raycast or Mouse.Target to my script.

Here’s my code is SCS

local inputservice = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local mouse = game.Players.LocalPlayer:GetMouse()

local PlayerGui = player:WaitForChild('PlayerGui')
local GameGUI = PlayerGui:WaitForChild('Door')

local Activ = false
local Obj = nil
local Distance = 5

inputservice.InputBegan:connect(function(Input)
	if Activ and Input.UserInputType == Enum.UserInputType.Keyboard and Input.KeyCode == Enum.KeyCode.E then
		for _,Door in pairs(workspace.Doors:GetChildren()) do
			Obj.Event:FireServer()
		end
	end
end)

while wait() do
	Activ = false
	Obj = nil
	GameGUI.Frame.Visible = false
	for _,Door in pairs(workspace.Doors:GetChildren()) do
---I tried doing this with raycast and mouse.Target but nothing worked
		--if mouse.Target and mouse.Target.Name == "Door" then    
		local DistanceFromCharacter = player:DistanceFromCharacter(Door.Door.Position)
		if math.floor(DistanceFromCharacter) <= Distance then
			GameGUI.Frame.Visible = true
			Activ = true
			Obj = Door
		end
		---end
	end
end

Door model:
image

If we can somehow figure this out, I’d gladly make a post in community resources about it!
Thanks for your time!

If its A first person game just ray cast from head but if its 3d use this [Release] Mouse Module

2 Likes

It’ll be first person, but I couldn’t find any way to code it.

SO what you want to do is use raycast from a certain distance if hit then it will run a code which is that Open and close gui

1 Like

You can just use ClickDetector.MouseHoverEnter and ClickDetector.MouseHoverLeave to know when the cursor hovers over the door. You don’t need complex raycasting or mouse.Target to do this.

2 Likes

I tried that (WorldRoot | Documentation - Roblox Creator Hub)
But If I put this in my code which has a for loop, it wont work.

1 Like

Ill see what I can do, thanks!

@RontheDragon204 just gave a good idea and the object ur using is decrpected its not roccmened

1 Like

Use workspace:Raycast() instead

1 Like

Thank you so much!
You have my Gratitude! :smiley:

No problem, I’m always glad to help out others. Please mark the post as solution so others can use the solution for their uses too! :smiley:

1 Like