Changing The Text On A TextButton When You Touch A Part

Hello, I want to make it so that when you touch a part, it changes the text inside of a TextButton, is there a way to do this? Also, I am quite new to scripting (as you can probably tell by this post) so if you could give an explanation that would be great. Hopefully that isn’t to much to ask, thank you!

2 Likes

Yeah it is possible, I’ll give an example:

local Part = script.Parent

Part.Touched:Connect(function()
    workspace.TextPart.BillboardGui.TextLabel.Text = "Changed Text!"
end)

Alright, let’s break this down here :L

local Part = script.Parent
So the first thing we’re doing is defining our Part (Or in this case, the script’s Parent which is also the Part)

Part.Touched:Connect(function()
Since we’re talking about a BasePart or regular Part, we’re creating a Touched Event with it so that whenever it gets touched, the remaining lines we put inside this function will be run

workspace.TextPart.BillboardGui.TextLabel.Text = "Changed Text!"
So, I’m pretty sure Billboard Gui’s are Gui’s that can be attached to a part, and so we also put a TextLabel inside the Gui (We’re also changing the “Text” property to change the current text that it’s on)

end)
And we use the end) to of course, end the Touched function/event

I think this is prob the best I can explain it, if you have any more questions lemme know :slightly_smiling_face:

Hmm, didn’t quite work for me. I have the TextButton in StarterGUI, do I need to put the script somewhere else since at the moment I have it in the part that you touch. Sorry!

Ah, alright then in this instance you’d need to configure around with the Player’s GUI then:

So adding onto our sample code here:

local Part = script.Parent

Part.Touched:Connect(function(Hit)
    if Hit.Parent then
        local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
        if Player then
            Player.PlayerGui.TextButton.Text = "Changed Text!"
        end
    end
end)

If you need me to break it down here feel free to ask me :sweat_smile:

2 Likes

I think we are almost there! It keeps saying that TextButton is not a valid member of PlayerGUI.

Ah y e s, you need to Parent that into what your Frame is supposed to be
So maybe uh:

local Part = script.Parent

Part.Touched:Connect(function(Hit)
    if Hit.Parent then
        local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
        if Player then
            Player.PlayerGui.Frame.TextButton.Text = "Changed Text!"
        end
    end
end)

Just replace the Frame.TextButton with wherever your Frame & TextButton is at

3 Likes

You would need to include the ScreenGui when looking for the Frame.

AH YES, SCREENGUI INSIDE A FRAME INSIDE A TEXTLABEL
All of this Parenting stuff is confusing me, but yeah it should be ScreenGui.Frame.TextLabel.Text

2 Likes

Yes! Thank you so much for the help! It really means a lot! :smiley:

3 Likes