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)
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!
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.