Most efficient way to make a Gui appear when player touched a brick/part?

Hello Devs! I was just wondering how I could make a Gui appear when touching a part. I thought this would be very helpful! Thanks. Please apply a script or explanation. Thanks.

Edit: 13/07/24 (this is very old and i had no idea how to script, I’m sure there are MUCH better threads to get info abt gui on touch from than this! You could probably find a script below and add a debounce feature to stop flickering)

15 Likes

Sorry if this is already a topic. :sweat_smile:

3 Likes

All you have to do is make a ScreenGUI in the StarterGUI and add everything else and then after that you should add a part so that we know thats the part we are aiming to touch.

Next, create a script on the Part making sure the script is the parent of the part and once you have done that add this to the script.

local Part = game.Workspace.Part

local plr = game.Players.LocalPlayer

local PlayerGUi = game.Players.plr:FindFirstChild(“PlayerGUI”)

local ScreenGUI = game.Players.plr.PlayerGUi:FindFirstChild(“ScreenGUI”)

local Frame = game Playees.plr.PlayerGUI.ScreenGUI:WaitForChild(“Frame”)

Once you have called everything create a function so that we know if the player has touched the part then the GUI will appear.

script.Parent.Touched:Connect(function(hit)
if hit then
local player = hit.Parent
if player then
Frame.Visible = true

end

end

end)

Note: If theres any error here then let me know because I typed this by heart.

12 Likes

Bruh, how can you call yourself a scripter when calling a variable with an event such as PlayerAdded,

And how can you not know about FilteringEnabled ahah

And you also must know that screengui has also no Visible property.

Anyway, @gorillasIayer here is the right code:

Create your ScreenUI, and create a frame or some UI element inside that.
Then create a localscript, inside this last UI element you created and paste this inside:

local part = workspace.Part;
local plr = game.Players.LocalPlayer;
local ui = script.Parent;

part.Touched:Connect(function(hit)
   if plr.Character ~= nil and hit.Parent == plr.Character then
      ui.Visible = true; 
   end 
end)

Don’t hesitate to ask me anything!
Sincerly

15 Likes

Try this:

local player = game.Players.LocalPlayer
local part = script.Parent
local gui = game.StarterGui.ScreenGui.Frame

part.Touched:Connect(function(part)
gui.Visible = true
gui:Clone() – bug measures
end
end

4 Likes

Oh, and in case you wanted to turn it invisible when leaving the part you can add this after what I did before:

part.TouchEnded:Connect(function(hit)
   if plr.Character ~= nil and hit.Parent == plr.Character then
      ui.Visible = false
   end
end)
3 Likes

In my experiences with TouchEnded, it hasn’t always been reliable. It could have been reworked since I last used it in 2013 though.

4 Likes

That’s not false. I usually make myself a custom function for that using the method :GetTouchingParts(). However, I chose a simple way to explain this to a new player, and TouchEnded even if not reliable is still a thing :slight_smile:

3 Likes

Oh my bad! I forgot to add local player not playeradded and it is the Frame that has the visible property.

Also, I don’t think you don’t need the ; in the Lua programming language.

You don’t need the ; this is correct. I use it from years of PHP, C, Java, and other languages. It’s just a nice clean way of ending a line.

3 Likes

Yup exactly. ScreenGui’s are here only to hold other UI elements which include the Visible property. And yup, however LocalPlayer only works on the local side of the game, and meant to use in localscripts.

and for the ; well it’s useless in lua but it’s an habit I got from writing in other languages. ; is optional in js too, but mandatory in php for example.

5 Likes

I went to sleep early tonight, thanks for all the help.

When I do what you said, whenever I took steps on the part it flickered on and off.

As @MrLonely1221 said, and I agreed the Event TouchEnded is not reliable. It is meant to fire when the collision between two bricks (in this case you, and the part) ends.

However it fires way too often, as collisions are unstable between both.

Remove the part of the code including the function fired by the TouchEnded event, and I advise you create a close button in your UI element.

Or, more complicated you can make your own TouchEnded function but it’s more likely badly optimized.

5 Likes

Yeah, I understand. Thank you for your contribution.

Hey, sorry for being THAT late, but is there a way to make it so when a player touches a part, the GUI appears for everyone in the server and not just the player who touched it? I know a single bit about Lua U (i’m a beginner) and i don’t know how to make what i just said. Do you have any idea on how to do that?

Here you Go


local Part = game.Workspace.YourTouchPart
local Players = game.Players 


function EveryoneUI()

      for _,v in pairs(Players:GetPlayers()) do 

            v.PlayerGUI.ScreenGUI.Enabled = true

     end
end



Part.Touched:Connect(function (Hit)

      if Hit.Parent:FindFirstChild("Humanoid") then

          Print("Humanoid")
          EveryoneUI()

      end
end)

Server Script

2 Likes