Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
-1

Gui opens when player touches a brick?

Asked by
steev 0
11 years ago

When the player touches a brick a GUI opens up on their screen for about 5 seconds and then fades away.

Can i have an answer in Lua please?

2 answers

Log in to vote
3
Answered by 11 years ago

This is an interesting question. It would be relatively simple to implement this incorrectly, but requires a bit of a process to do it the correct way. Since we want to comply with FilteringEnabled, the following method is the best.

First of all, create your GUI in StarterGui. Then, put a RemoteEvent in it, called "Show". On the same level, include a LocalScript. Your hierarchy structure should look like this:

  • StarterGui
    • ScreenGui
      • TextLabel (This is the GUI we will be showing)
      • LocalScript
      • Show (RemoteEvent)

Inside of your brick, insert a Script.

  • Workspace
    • Part
      • Script

RemoteEvents allow you to send data from the Server to the Client, and vice-versa. This is very useful when FilteringEnabled is enabled!

Inside the brick script, include the following:

01script.Parent.Touched:connect(function(p)
02    pcall(function() --We will use pcall here because something could very well go wrong, and we don't want our script to break
03        if not p.Parent then return end
04 
05        local player = game.Players:playerFromCharacter(p.Parent)
06 
07        if not player then return end
08 
09        player.PlayerGui.ScreenGui.Show:FireClient()
10    end)
11end)

Now, what the above code will do is check whatever touches the part is a player, and if so, try to fire the RemoteEvent inside of the GUI.

Now, inside of the LocalScript (which is in the GUI), we will use this code:

01local gui = script.Parent.TextLabel
02local working
03script.Parent.Show.OnClientEvent:connect(function()
04    if working then return end -- We don't want to start fading if we're already fading
05    working = true
06    gui.Visible = true
07    for i=1, 0, -0.1 do
08        gui.BackgroundTransparency = i
09        gui.TextTransparency = i
10        wait(0.08)
11    end
12    wait(5)
13    for i=0, 1, 0.1 do
14        gui.BackgroundTransparency = i
15        gui.TextTransparency = i
16        wait(0.08)
17    end
18    gui.Visible = false
19    working = nil
20end)

When you put all of this together, you should get a button that properly fades in a GUI when a player touches the part.

Ad
Log in to vote
-1
Answered by 10 years ago

StarterGui = game.StarterGui function onTouched(hit) wait .5 StarterGui.ShopGUI.ShopFrame.Visible = true StarterGui.ShopGUI.Open.Visible = false end connection = script.Parent.Touched:connect(onTouched)

It should open if not do tthis

StarterGui = game.StarterGui function onTouched(hit) wait(.5) StarterGui.ShopGUI.ShopFrame.Visible = true StarterGui.ShopGUI.Open.Visible = false end connection = script.Parent.Touched:connect(onTouched)

Answer this question