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
9 years ago

So, I tryed this:


debounce = false function onTouched(hit) if debounce == false then debounce = true game.StarterGui.ScreenGui.TextLabel.Visible = true debounce = false end end script.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
9 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.

debounce = false

function onTouched(hit)
    local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
    if plr and debounce == false then
        debounce = true
        --Use WaitForChild in case the PlayerGui is still loading.
        plr:WaitForChild("PlayerGui").ScreenGui.TextLabel.Visible = true
        debounce = false
    end
end

script.Parent.Touched:connect(onTouched)
0
It appears I posted a bit slow, lol. alphawolvess 1784 — 9y
0
What does the debounce mean funnytevinn -5 — 4y
Ad
Log in to vote
0
Answered by 9 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.

db = false

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

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