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
10 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 10 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:

script.Parent.Touched:connect(function(p)
    pcall(function() --We will use pcall here because something could very well go wrong, and we don't want our script to break
        if not p.Parent then return end

        local player = game.Players:playerFromCharacter(p.Parent)

        if not player then return end

        player.PlayerGui.ScreenGui.Show:FireClient()
    end)
end)

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:

local gui = script.Parent.TextLabel
local working
script.Parent.Show.OnClientEvent:connect(function()
    if working then return end -- We don't want to start fading if we're already fading
    working = true
    gui.Visible = true
    for i=1, 0, -0.1 do
        gui.BackgroundTransparency = i
        gui.TextTransparency = i
        wait(0.08)
    end
    wait(5)
    for i=0, 1, 0.1 do
        gui.BackgroundTransparency = i
        gui.TextTransparency = i
        wait(0.08)
    end
    gui.Visible = false
    working = nil
end)

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 9 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