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

How do I open a GUI by a part touch?

Asked by
lucas4114 607 Moderation Voter
10 years ago

So, I tryed this:

01debounce = false
02 
03function onTouched(hit)
04    if debounce == false then
05        debounce = true
06        game.StarterGui.ScreenGui.TextLabel.Visible = true
07        debounce = false
08    end
09end
10 
11script.Parent.Touched:connect(onTouched)

It didn't work, I don't know why, I've made GUIs before that can open and close by a GUI button. This is my first time making a GUI open by a part touch....

2 answers

Log in to vote
0
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
10 years ago

This mistake is made a lot.

Making you changes in StarterGui won't work. StarterGui is not what you see! It is simply a holder or a container that you use to store guis.

When you join the game and each time your respawn, everything in StarterGui is cloned into PlayerGui. This is what you see. (You can only see stuff in StarterGui in studio for testing purposes. In-game, you view PlayerGui.)


So to solve your problem, we must make our changes in that specific player's PlayerGui. Each player has his or her own PlayerGui, so we must make sure we get the correct one.

The hit parameter will help us out. If a true player character touched the brick, then hit.Parent must be his or her Character object. Now that we have the character, we can get the Player.

We could make a search through Players service with FindFirstChild, but there's a better method. We can use GetPlayerFromCharacter. This method of the Players service will take the Character given in the parentheses and find its matching Player. It will then return that Player -- or nil if a valid Character was not given. In case it returns nil, an extra check must be made.

01debounce = false
02 
03function onTouched(hit)
04    local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
05    if plr and debounce == false then
06        debounce = true
07        --Use WaitForChild in case the PlayerGui is still loading.
08        plr:WaitForChild("PlayerGui").ScreenGui.TextLabel.Visible = true
09        debounce = false
10    end
11end
12 
13script.Parent.Touched:connect(onTouched)
0
It appears I posted a bit slow, lol. alphawolvess 1784 — 10y
0
What does the debounce mean funnytevinn -5 — 5y
Ad
Log in to vote
0
Answered by 10 years ago

I would assume it's because you're manipulating the GUI inside of the StarterGui when you should try to manipulate the GUI inside of PlayerGui.

Now, we are still using a Script which is the Child of SpecialPart which will go to ScreenGui, Frame and make this visible.

Here will be our code (I have tested this and it works), look bellow for information about it.

01db = false
02 
03script.Parent.Touched:connect(function(Hit) -- This is how I set up functions, your way is still fine!
04    if db == false then db = true
05        local player = game.Players:GetPlayerFromCharacter(Hit.Parent)
06        player.PlayerGui.ScreenGui.Frame.Visible = true
07        wait(2)
08        db = false
09    end
10end)

We are using ':GetPlayerFromCharacter()' to do what the name states, get the Player from the Character who touched. In the parenthesis, we put the character, which will be Hit.Parent. Now that we made it to the Player who touched the part, we go to their PlayerGui and make the GUI visible for use. You should add a wait before you change your 'debounce' to false so no one can touch this and work until wait is over, your way is visibly impossible to notice. Of course, you can do it your way, no errors will occur.

Answer this question