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

How to make a ScreenGUI appear when someone steps on a brick?

Asked by 8 years ago

I've tried to make a ScreenGUI appear when a player steps on a brick, but I failed a lot of times!

Here are my attempts: 1.

local Text = game.StarterGui.ScreenGui.Frame1.Page1
local Next = game.StarterGui.ScreenGui.Frame1.Next
local Dialog1 = game.Workspace.Dialog1

local function onTouch(Dialog1)
   Text.Visible = true
   Next.Visible = true  
end

Dialog1.Touched:connect(onTouch)

2.

local Text = game.StarterGui.ScreenGui.Frame1.Page1
local Next = game.StarterGui.ScreenGui.Frame1.Next
local Dialog1 = game.Workspace.Dialog1

local function SteppedOn(Dialog1)
   Text.Visible = true
   Next.Visible = true  
end

Dialog1.Touched:connect(SteppedOn)

2 answers

Log in to vote
3
Answered by 8 years ago

First off, two things:

  1. Both your attempts are the same thing, just with differently named functions. The name of the function won't matter as long as it's connected to an event. So I could even do GAHAHAHAHGJDHFIEMM for a name of my function and it'd still work.

  2. You're trying to change the StarterGui and that'll work, but it isn't updated. If the player died after touching this brick then he/she might see the text, but it'd never go away, unless there's a button to press. What you need to do, is get to the player's local PlayerGui. So let's do that.

Firstly, we need to make a function, or make an event. I'll do it both ways, you can decide how to do it.

--I'm going to name my function GAHAHAHAHGJDHFIEMM just to show the name won't matter
function GAHAHAHAHGJDHFIEMM(hit) --This word in parenthesis is an argument. I'll tell you about it.
    --We'll put some code in here later
end

Notice the word hit inside the parenthesis. That is an argument. It's a value to use for the function. With functions that use events, the events create the arguments, you just gotta learn what each event throws at you as an argument. In this instance, the touched event turns the first argument of any function that it's attached to, to an instance part. To be precise, the part that just touched it. So we can get the character by doing this:

function GAHAHAHAHGJDHFIEMM(hit)
    local HitChar = hit.Parent
end

Ok, now we got a variable inside the function getting the character that just touched it. What we need now is a way to get to the player. We know (Or I know, this may be new to you) that all character's models are named the same thing as their corresponding players, right? And all the players are stored in game.Players. How about we use that? But we need something that'll look for it and not break the script... Brackets won't work because they force finding it and break the script if it's ever called. So let's try using :FindFirstChild()

function GAHAHAHAHGJDHFIEMM(hit)
    local HitChar = hit.Parent
    local HitPlr = game.Players:FindFirstChild(HitChar.Name)
end

Now that we got :FindFirstChild() looking for the character's name inside game.Players, we need to make sure it actually found it before we start doing anything. Let's make an if statement and just simply say 'if hitplr then'

function GAHAHAHAHGJDHFIEMM(hit)
    local HitChar = hit.Parent
    local HitPlr = game.Players:FindFirstChild(HitChar.Name)
    if HitPlr then
        --More code will go here
    end
end

Now we confirmed there's an actual player, we can finally get to its PlayerGui. Now, the PlayerGui's setup is just like how you made it in StarterGui, unless scripts change certain things around. Assuming there aren't any scripts altering where your TextLabel is, we're going to get straight to it. Also, I'm going to add in "ScreenGui" because you need a ScreenGui to see any sort of GUI! If you just got a frame inside StarterGui, that won't work. We need a ScreenGui. Be sure to create one and put your GUI elements in there!

EDIT: I don't know how I missed this, but I realized you could just leave all the elements inside the frame visible, but you could have Frame1's visible property to be false. It'll hide any GUI elements inside it. I'm just going to make it true.

function GAHAHAHAHGJDHFIEMM(hit)
    local HitChar = hit.Parent
    local HitPlr = game.Players:FindFirstChild(HitChar.Name)
    if HitPlr then
        HitPlr.PlayerGui.ScreenGui.Frame1.Visible = true
    end
end

Tada! We just finished the function! Remember to put your Frame inside the ScreenGui. Unless Frame1 is a ScreenGui. I'm thinking it's a frame because of its name, but that doesn't mean it is. Now we need to link the function to an event. Like this:

function GAHAHAHAHGJDHFIEMM(hit)
    local HitChar = hit.Parent
    local HitPlr = game.Players:FindFirstChild(HitChar.Name)
    if HitPlr then
        HitPlr.PlayerGui.ScreenGui.Frame1.Visible = true
    end
end

script.Parent.Touched:connect(GAHAHAHAHGJDHFIEMM)

Now put this script inside the brick that you need to touch and it'll work!

Remember when I said "I'll do it both ways"? I'm about to show you another way. It makes the script more compact, but it may be a bit complex, so if you get confused, just use the first method.

script.Parent.Touched:connect(function(hit)
    local HitChar = hit.Parent
    local HitPlr = game.Players:FindFirstChild(HitChar.Name)
    if HitPlr then
        HitPlr.PlayerGui.ScreenGui.Frame1.Visible = true
    end
end)

See how I just morphed a function with the event connection? That's good until it says your last end isn't right. Just add a closing parenthesis and it should be fine. I like it this way because I don't have to name my functions, and it keeps the bottom of my scripts nice and tidy.

Here's a quick demonstration

If this helped, then accept my answer and we both get reputation points!

Good luck on your future Scripting Adventures! ~lightpower26

0
Wait a sec... but where should I put the whole brick and script into? starlebVerse 685 — 8y
0
Put the script inside the brick. The brick can go anywhere in workspace lightpower26 399 — 8y
Ad
Log in to vote
0
Answered by
rexbit 707 Moderation Voter
8 years ago

What you've done is that your trying to access the gui through StarterGui, If your going to modify any of the compartments in your gui or the gui itself you should access it through the PlayerGui.

PlayerGui

The PlayerGui is a copy of all the elements located within the Container, StarterGui. What PlayerGui does is it can be access and changed from the player's gui Perspective without messing with the fundamental gui inside of StarterGui.

Perfected Code

local Text = game.Players.LocalPlayer.PlayerGui.Frame1.Page1
local Next = game.Players.LocalPlayer.PlayerGui.Frame1.Next
local Dialog1 = game.Workspace.Dialog1

local function SteppedOn(Dialog1)
   Text.Visible = true
   Next.Visible = true  
end

Dialog1.Touched:connect(SteppedOn)


1
Again, that only works in a LocalScript. I'm going to assume that he's working in a regular Script. (Or a ServerScript, they're the same thing) lightpower26 399 — 8y

Answer this question