Lua Tutoring
Lua Tutoring

Reputation: 180

How do you open a GUI when you touch a brick? (with Filtering Enabled)

I'm trying to make a Shop enclosure when you touch a brick, it'll open the Shop Gui,

Now the main problem is that I do not know how to make the GUI open since using scripts whilst filtering enabled just won't cut it.

Does anyone have a solid explanation?

Upvotes: 0

Views: 1038

Answers (1)

NetherGranite
NetherGranite

Reputation: 2100

First of all, in order to execute any action when a brick is touched, you will need to use the .Touched attribute of your brick. Your brick has this attribute because it is a data type called a Part.

Second of all, I am not sure how you want the GUI to open, but the most basic way is to enable it using the .Active attribute of your GUI element. This will simply make it appear on screen. You GUI element has this attribute because it is a GuiObject, whether it be a Frame, TextButton, or whatever else.

The code will look something like this:

brick = path.to.part.here
gui = path.to.gui.here

function activateGui() --shorthand for "activateGui = function()"
    gui.Enabled = true
end

brick.Touched:connect(activateGui)

Notice that .Enabled is a boolean (true or false). Also, notice that .Touched is a special object with a :connect(func) function. This is because .Touched is actually an Event. All Events have a special :connect(func) function that take an argument of another function func which is to be executed when the event occurs. In this case, we asked the brick's .Touched event to execute activateGui when it occurs.

Also, .Enabled is set to true by default, so in order for this method to work, make sure you set it to false in ROBLOX Studio by unchecking .Enabled in the Properties tab for the GUI element. Note that you do not have to do this for every single element of the GUI; if you set .Enabled to false on a certain element, all of its children will also automatically be hidden, so you only have to do it on the parent element.

Finally, you must do this in a Local Script. Because the GUI is unique for every single player, it is actually handled by each player's computer, not the ROBLOX server itself. Local Scripts are scripts that are specifically handled by a player's computer and not the server, so it is crucial that you do not try to do this with a regular Script, which is handled by the server.

For your information, the above code can be condensed to this if you would like:

brick = path.to.part.here
gui = path.to.gui.here

brick.Touched:connect(function()
    gui.Enabled = true
end)

This is because you do not have to create a function, name it, and then give that name to .Touched; instead, you can just create it right on the spot.

Upvotes: 1

Related Questions