Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

GUI click script doesn't work? Can't identify issue [ANSWERED]

a = script.Parent.Text
b = script.Parent.Parent.ChoiceTwo.Text
c = script.Parent.Parent.ChoiceThree.Text
t = script.Parent.Parent.Textbox

function onButtonClicked()
	if a == "text1" then
		a = "text2"
		b = "text3"
		c = "text4"
		t = "text5"
		print("This button got clicked.")
	elseif a == "text2" then
		a = "text6"
		b = "text7"
		c = "text8"
		t = "text9"
	elseif a == "text6" then
		game.Players.LocalPlayer.Character.Health = 0
	elseif a == "text10" then
		game.Players.LocalPlayer.Character.Health = 0
	end
end

script.Parent.MouseButton1Click:connect(onButtonClicked)

(I placed filler text in the quotation marks instead of out-of-context quotes. The numbers are mostly arbitrary.)

When this button is clicked, it should change the text of other textboxes and its own text. However, when I test it out, something is wrong. When clicked, the text doesn't change, but the Output prints "This button got clicked." and any clicks after that don't do anything. There are no other errors in Output. Why does it skip over the text edits and go straight to print("This button got clicked.")?

2 answers

✓ Accepted answer

If you set a variable to a field, that variable only stores the field's contents as it were; when you re-set the variables later, the fields they were set to earlier are not updated.

For example, this code:

a = script.Parent.Text
a = "new text"

won't work. You must do this:

a = script.Parent
a.Text = "new text"

1 comment

0

Thank you, it works now!

When you say a = script.Parent.Text, you are setting a to the property's (Text's) value, not the property Text. Therefore, when you want to access this property, you have to use a = script.Parent and then a.Text = "Whatever you want" everywhere you want to change its text. Take a look.

a = script.Parent
b = script.Parent.Parent.ChoiceTwo
c = script.Parent.Parent.ChoiceThree
t = script.Parent.Parent.Textbox

function onButtonClicked()
    if a.Text == "text1" then
        a.Text = "text2"
        b.Text = "text3"
        c.Text = "text4"
        t.Text = "text5"
        print("This button got clicked.")
    elseif a.Text == "text2" then
        a.Text = "text6"
        b.Text = "text7"
        c.Text = "text8"
        t.Text = "text9"
    elseif a.Text == "text6" then
        game.Players.LocalPlayer.Character.Health = 0
    elseif a.Text == "text10" then
        game.Players.LocalPlayer.Character.Health = 0
    end
end

script.Parent.MouseButton1Click:connect(onButtonClicked)

This discussion is preserved as it was. New answers, comments, and votes are closed.