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

How do I change the text of a textbox to the value of a variable?

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:

If Workspace.PartScoreboard.SurfaceGui.Frame.Pin1.Backgroundcolor3 = color3.new(1, 0, 1)
Workspace.Scorekeeper.SurfaceGui.frame.Round1.Text.new ((score))
end

2 comments

1

Text is a property, like Backgroundcolor3. Therefore, gui.Text = textvar. Of course, textvar needs to be a string (or convertable into a string, such as a number).

1

You also need two == and lowercase if and a then.

2 answers

✓ Accepted answer

Lua is case sensitive, so you need to be careful. if should always be lowercase. To access the Workspace, the following are acceptable:

game.Workspace
game:GetService("Workspace")
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:

if (_____) then
	-- Code if ____ is true
else
	-- Code if ____ is false
end

To check if something is the same as something else you can use ==

local num = 2
if 2 == num then
	print("num is 2")
else
	print("num is not 2")
end

Here's the finished product:

if workspace.PartScoreboard.SurfaceGui.Frame.Pin1.BackgroundColor3 == Color3.new(1, 0, 1) then
	workspace.Scorekeeper.SurfaceGui.frame.Round1.Text = score
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 .

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.

guiObject.Text = 5

Using variables

Variables can be used to store a string or non-string as described.

local score = 5
guiObject.Text = score

Concatenation and string manipulation

Additionally, string manipulation - frequently concatenation - can be used.

local score = 5
guiObject.Text = "The score is: " .. score

Or, with another variable:

local score = 5
local scorePrefix = "The score is: "

guiObject.Text = scorePrefix .. score

The Final Result

local frame = workspace.PartScoreboard.SurfaceGui.Frame --the path of the frame storing the guis
local score = 5 --the score, updated in other code

if frame.Pin1.BackgroundColor3 == color3.new(1, 0, 1) then --the conditional, as given, debugged
	frame.Round1.Text = score --the assignment of the text
end

It is important to use conditional statements (ifs) 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.

1 comment

0

That was slightly confusing, but thank you for you're answer ^^'

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