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