I'm attempting to change Workspace.Scorekeeper,SurfaceGui.Frame.Round1's text to the score value Score=0 to 90
But my attempts have failed.
How exactly do I change the text of a textbox to something else? Attempt:
1 | If Workspace.PartScoreboard.SurfaceGui.Frame.Pin 1. Backgroundcolor 3 = color 3. new( 1 , 0 , 1 ) |
2 | Workspace.Scorekeeper.SurfaceGui.frame.Round 1. Text.new ((score)) |
3 | end |
Lua is case sensitive, so you need to be careful. if
should always be lowercase. To access the Workspace, the following are acceptable:
1 | game.Workspace |
2 | game:GetService( "Workspace" ) |
3 | workspace |
BackgroundColor3
should always have proper case as well:
As GoldenPhysics stated, Text
is a writable property. That means you can set it using =
. The only time you can't set properties is if they are read-only.
You should also make sure that your conditionals are formatted as such:
1 | if (_____) then |
2 | -- Code if ____ is true |
3 | else |
4 | -- Code if ____ is false |
5 | end |
To check if something is the same as something else you can use ==
1 | local num = 2 |
2 | if 2 = = num then |
3 | print ( "num is 2" ) |
4 | else |
5 | print ( "num is not 2" ) |
6 | end |
Here's the finished product:
1 | if workspace.PartScoreboard.SurfaceGui.Frame.Pin 1. BackgroundColor 3 = = Color 3. new( 1 , 0 , 1 ) then |
2 | workspace.Scorekeeper.SurfaceGui.frame.Round 1. Text = score |
3 | end |
Text is a Property
Text is a property of a GuiObject, just like BackgroundColor3, Position, and Size.
Literal string
Therefore, to set it, just get the property of the gui guiObject.Text
and set it equal to the string you want .
1 | guiObject.Text = "the score is 5" |
Non-string conversion to string
It is also possible to use non-strings if they will automatically convert to a string, such as numbers, or if manually set to a string using the tostring()
method.
1 | guiObject.Text = 5 |
Using variables
Variables can be used to store a string or non-string as described.
1 | local score = 5 |
2 | guiObject.Text = score |
Concatenation and string manipulation
Additionally, string manipulation - frequently concatenation - can be used.
1 | local score = 5 |
2 | guiObject.Text = "The score is: " .. score |
Or, with another variable:
1 | local score = 5 |
2 | local scorePrefix = "The score is: " |
3 |
4 | guiObject.Text = scorePrefix .. score |
The Final Result
1 | local frame = workspace.PartScoreboard.SurfaceGui.Frame --the path of the frame storing the guis |
2 | local score = 5 --the score, updated in other code |
3 |
4 | if frame.Pin 1. BackgroundColor 3 = = color 3. new( 1 , 0 , 1 ) then --the conditional, as given, debugged |
5 | frame.Round 1. Text = score --the assignment of the text |
6 | end |
It is important to use conditional statements (if
s) correctly. All keywords are lowercase (if
not If
), double equals ==
is used to compare while singe equals=
is used for assignment, and all ifs require then
to close the conditional.