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:

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

The Console Prints "--" but when i do

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

Its Print the Text thats inside

How can i fix this?

3 answers

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

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:

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

You would do this:

local User = script.Parent.Parent.TextBox

Then when you were printing the text you would do:

print(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.

local 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.

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

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