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

How to access a GUI in a script

I'm trying to make a GUI pop up when you step on this brick, holding a gun. I already have the GUI, and the script (Thanks MrNicNac), but I don't know how to access the GUI. Here's the script:

local toolname = "Luger, Colt 45"
script.Parent.Touched:connect( function(hit) 
 if hit.Parent then 
 local player = Game.Players:GetPlayerFromCharacter(hit.Parent) 
if player then 
if player.Character:FindFirstChild(toolname) then 
-- Where GUI goes
 end 
 end 
 end 
end) 

And plus, is there a way to make it only once, or once in a certain amount of time?

2 answers

Using the code you have, you would access the player's PlayerGui simply by going player.PlayerGui.MYGUI

"And plus, is there a way to make it only once, or once in a certain amount of time?"

Edit: Yes, indeed there is. In order to do this, you'll need to use some type of object that is to be looked for when the event fires. In this case, I'm using a IntValue.

local toolname = "Luger, Colt 45"
script.Parent.Touched:connect( function(hit) 
	if hit.Parent then 
		local player = Game.Players:GetPlayerFromCharacter(hit.Parent) 
		if player and not player:FindFirstChild("No") then 
			if player.Character:FindFirstChild(toolname) then 
				local gui = player.PlayerGui.MyGUI
				local no = Instance.new("IntValue", player)
				no.Name = "No"
				Spawn(function()
					wait(10) -- Player can only use the button once every 10 seconds
					no:Destroy()
				end)
			end 
		end 
	end 
end) 

Line 10 basically allows the code to continue running while the scope (the function) runs at it's own pace. You can get this effect a variety of ways, using game.Debris:AddItem(no, 10) or by using the delay function.

Good luck!

Put at the top

debounce = false

In the script.

If debounce == false then 
debounce = true

To access the GUI. Do:

player.PlayerGui.GUINAME

At the end, put

debounce = false

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