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

How to access a GUI in a script

Asked by 11 years ago

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:

01local toolname = "Luger, Colt 45"
02script.Parent.Touched:connect( function(hit)
03 if hit.Parent then
04 local player = Game.Players:GetPlayerFromCharacter(hit.Parent)
05if player then
06if player.Character:FindFirstChild(toolname) then
07-- Where GUI goes
08 end
09 end
10 end
11end)

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

2 answers

Log in to vote
0
Answered by
nate890 495 Moderation Voter
11 years ago

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.

01local toolname = "Luger, Colt 45"
02script.Parent.Touched:connect( function(hit)
03    if hit.Parent then
04        local player = Game.Players:GetPlayerFromCharacter(hit.Parent)
05        if player and not player:FindFirstChild("No") then
06            if player.Character:FindFirstChild(toolname) then
07                local gui = player.PlayerGui.MyGUI
08                local no = Instance.new("IntValue", player)
09                no.Name = "No"
10                Spawn(function()
11                    wait(10) -- Player can only use the button once every 10 seconds
12                    no:Destroy()
13                end)
14            end
15        end
16    end
17end)

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!

Ad
Log in to vote
0
Answered by 11 years ago

Put at the top

1debounce = false

In the script.

1If debounce == false then
2debounce = true

To access the GUI. Do:

1player.PlayerGui.GUINAME

At the end, put

1debounce = false

Answer this question