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

How do I change text on a StarterGui?

Asked by 5 years ago

Hello All

I'm stuck with trying to change the text label in the starter gui. The text label sits in the following hierarchy

Text label is child of a frame The frame is the child of a screengui The screengui is the child of the Starter Gui

I'd like the text in the text label to change when the player hits a brick. The script I am using to do this is below.

The problem is that the text doesn't change when my player hits the brick. I know the touch event is being triggered, becase the seconbd print statement is printing out "You hit the brick". I also know that I have the correct path for the text label as the first print statement is printing out successfully.

Could someone advise me what I am doing wrong here?

Thanks ever so much for your help

Chris

********* Script is here *************************

local txtlbl=game.StarterGui.ScreenGui.Frame.TextLabel print(txtlbl.Text)

script.Parent.Touched:connect(function(part)

if part.Parent:FindFirstChild("HumanoidRootPart") then print ("You hit the brick") txtlbl.Text="There is a change in text" end end)

0
Block codes exist for a reason.. Also you should change text on the player gui not starter gui greatneil80 2647 — 5y
0
Thank you Neil. Can I check what you mean by block codes? Do you mean the indents which occur within loops and if/then blocks? redchris999 4 — 5y

2 answers

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

So basically, the StarterGui performs in a way which is like saying, “these are my starting GUIs when I spawn.” If you change the text in the ScreenGui, when it is in StarterGui, it will only give new players that, and all of them.

The way of doing this is by getting PlayerGui. PlayerGui is the StarterGui, for just one player. Say I want to do exactly what you’re doing: I would use this script:

script.Parent.Touched:connect(function(part)
     local player = game.Players:GetPlayerFromCharacter(part.Parent) —this is our gateway to getting the PlayerGui object.
     if player then
          print(“verified player”..player.UserId)
          —this is good, this basically checks if we can get the player from the character.  If we can, we will print that line above.  If not, we will yield the function.  It’s also good to identify the player by their UserId in the print line, so you can make sure.
          local PlayerUI = player:WaitForChild(“PlayerGui”)
          local txtLabel = PlayerUI[“DesiredGuiName”].TextLabel
          txtLabel.Text = “You touched the brick!”
     end
end)

I really hope this helped, I was sitting here trying to find good words to explain. It’s a semi-hard thing to explain, haha. Trust me, when I started developing last year I was making way worse mistakes. Don’t take this as something bad, but instead take it as something good. You made this mistake, but it will help you drastically in the future.

Let me know if this helped. Thanks!

0
Hi Maximum. It did work thank you. It took me a minute to work out that "DesiredGuiName" was a placeholder. The actual gui is txtlabel to "PlayerUI.ScreenGui.Frame.TextLabel", and once I used that then the text changed as I hoped. I don't have enough reputation to upvote you I'm afraid, but thank you anyway. redchris999 4 — 5y
0
Hi Maximus. It did work thank you. It took me a minute to work out that "DesiredGuiName" was a placeholder. The actual gui is txtlabel to "PlayerUI.ScreenGui.Frame.TextLabel", and once I used that then the text changed as I hoped. I don't have enough reputation to upvote you I'm afraid, but thank you anyway. redchris999 4 — 5y
Ad
Log in to vote
0
Answered by 5 years ago
script.Parent.Touched:Connect(function(h) 
    hum = h.Parent:FindFirstChild("Humanoid") 
    if hum then 
        check = game.Players:FindFirstChild() 
        for i, v in pairs(check) do 
            if v.Name == h.Parent.Name then 
                v.PlayerGui.ScreenGui.Frame.TextLabel.Text = "DesiredText"
            end
        end
    end
end)

Try this instead (I typed it up and tested it in studio so it should work)! :)

Your script is a little unorganized. You should not have added printing in your variable, it'll mess everything up and cause errors. By the way, the i, v in pairs loop was to ensure that the text change would be going into the player who touched it.

0
Thank you Andreaaaxo. I ran this and got an error "Argument 1 missing or nil" on line 7. I put in "Humanoid" as an argument but got another error ie "Bad argument #1 to pairs (table expected, got nil). redchris999 4 — 5y

Answer this question