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

TextBox Input dont work in a Script? Dont printing with a variable

Asked by 5 years ago

I have a Problem with a little Script:

1local User = script.Parent.Parent.TextBox.Text
2script.Parent.MouseButton1Click:Connect(function(player)
3    print(User)
4end)

The Console Prints "--" but when i do

1local User = script.Parent.Parent.TextBox.Text
2script.Parent.MouseButton1Click:Connect(function(player)
3    print(script.Parent.Parent.TextBox.Text)
4end)

Its Print the Text thats inside

How can i fix this?

3 answers

Log in to vote
0
Answered by 5 years ago
1local User = script.Parent.Parent.TextBox
2script.Parent.MouseButton1Click:Connect(function(player)
3    print(User.Text)
4end)

There's an important distinction to be made between what local User = script.Parent.Parent.TextBox and local User = script.Parent.Parent.TextBox.Text do in the code.

The first does a single action, it stores a reference to script.Parent.Parent.TextBox.

The second does two actions. First, script.Parent.Parent.TextBox.Text access the property of the TextBox. This is a string. Then it stores the variable. So the user variable in the second statement only stores the string, it does not keep a dynamic link to script.Parent.Parent.TextBox.Text. It stays the same. That's why we access the property in the event connection instead.

0
Actually, he uses the Text property in both of his scripts. The real problem lies in where he assigns the variable and what he is assigning that variable to. M39a9am3R 3210 — 5y
0
I am aware. The two versions of what the variable User can be come from me. In my script I have a expression for User that he didn't use. I was simply pointing out why I made the change. User#18718 0 — 5y
Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

Instead of:

1local User = script.Parent.Parent.TextBox.Text

You would do this:

1local User = script.Parent.Parent.TextBox

Then when you were printing the text you would do:

1print(User.Text)
Log in to vote
0
Answered by
M39a9am3R 3210 Moderation Voter Community Moderator
5 years ago

Problem

The problem is one script is assigning a string (the TextBox's Text) to a variable which never gets updated. The script is run remembering the value of the TextBox's Text.

1local User = script.Parent.Parent.TextBox.Text -- ==> "" (String) [It's an empty string.]

This result gets remembered in the script and when your event executes, you're telling it to print out that remembered result. Which would be an empty string.


Solution

You can do this in a variety of ways. You can change the script to how you have it where it goes through the TextBox and Text property directly, or you can reference the TextBox as an object prior to your event and reference the object to get to the Text property.

1local User = script.Parent.Parent.TextBox -- ==> TextBox (object) [It's a reference to the object you have set up.]
2script.Parent.MouseButton1Click:Connect(function()
3    print(User.Text)
4end)

Hopefully this solved your problem. If it did, hit the accept answer button. If it didn't please ask your questions in the comments and I'd be able to help you.

Answer this question