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

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

Log in to vote
0
Answered by
nate890 495 Moderation Voter
10 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.

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!

Ad
Log in to vote
0
Answered by 10 years ago

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

Answer this question