Difficult time trying to figure out how to show GUI when touching a part and then hiding it when not touching the part

Hello everyone!

I am trying to show a GUI when touching a part and then hiding the GUI when not touching the part. It’s your typical "Press E to …’ interface and I will be having multiple places where this can happen.

I have tried using Part.Touched events alongside Part.TouchEnded events, but there’s the issue where the touchended event is triggering even when touching the part.

I have tried ray casting downward to get the name of the part, which works, but I’m not sure what the best way is to handle multiple parts that can be touched, each having it’s own GUI. For example, I have a folder in my workspace that holds the parts that trigger GUI pop ups and when a ray hits a part in that folder, I show the GUI. This works great for one GUI, but what about as I expand and add more?

Now, I come from a programming background and I’m trying to architect this in a sort of self-fulfilling way. What I mean is, I’m imagining ray casting downward and finding the part and seeing if it exists in my folder (which works as of right now). Then, I feel like what should happen next is triggering something on the part that has it run it’s own showGUI function. That way, all I have to do down the road is add a part to that folder and then add the script for showing the GUI, and it’ll all just work.

I hope I’m making sense.

If it helps, here are what I have now:

My folder structure (the folder ‘Interactive Floors’ is the folder I’ve been talking about)

Here is the ray tracing code:

Honestly, I guess I’m just looking for guidance in where I should put the code for all this to work correctly.

1 Like

What i would do is utilize one script which would simplify things in theory to a great extent. Then use a table which can contain another table with 2 items, a part and a gui associated with it, run one loop to fire a ray as you have be doing but instead use the FindPartOnRayWithWhitelist so we pass a table of parts(a table that we only want to be detected) as our argument.

Here’s an example of what i mean

local PartsAndGui {
 {Part, Gui}

local WhitelistParts = {}

for _, Value in pairs(PartsAndGui) do ----iterate through PartsAndGui and add all parts to the WhitelistParts  table
     table.insert(WhitelistParts, Value[1])

while true do 
   local newRay = Ray.new(HumanoidRp.CFrame.p, Vector3.new(0,-1,0).unit * 10)
   local Hit = game.workspace:FindPartOnRayWithWhitelist(newRay,WhitelistParts  )

          if Hit then   
          local Gui --unkown???

        for _, Value in pairs(PartsAndGui) do -----Go back through the PartsAndGui table to find the Gui we need to show
               if Value[1] == Hit  then
                Gui = Value[2] --- now it's known
             break  -- we found the Gui so we don't need to finish iterating through the table
        -----Now show the gui or do whaterver with it

As alternatives to using rays you can look into something like Region3s, Zone-Checks, or even if you want to the GetTouchingParts method ( although some of these come with their disadvantages)

@Jaycbee05 Thank you for the reply!

That’s basically the route I was thinking of taking, but how do you solve needing to do an action based on which GUI is shown? Like, if I’m standing on a part called Teleport Inside, how would I have that part listen for the E key and run its associated function, rather than having it run, let’s say, the Teleport Outside part’s function?

I realize I could probably do this with some if/else statements, but I thought maybe there would be a way to just say, “You’re stepping on this part, run its associated script/event.”

1 Like

I tend to use this for a better hitbox. Make a part, then just detect if the magnitude is sufficient.

Edit: I’ve just read your previous post, and you can press, “E” and then pass an if statement to see if the magnitude is sufficient, then do something

@TOP_Crundee123 I’d like the GUI to show up regardless of the magnitude. You can be moving or standing still, but as long as you’re on the part, show the GUI and hide it if you’re not. Would detecting magnitude work for that?

Yes, if magnitude is <= to a certain amount you can open the GUI