Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Gui opens when player touches a brick?

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

✓ Accepted answer

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.

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.