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

Why doesn't TextBounds.X not work?

This is supposed to check if the current X TextBounds is bigger, and if it's bigger, it should change the variable to the new X TextBounds. However this isn't changing the value. I'll show you why below.

--NewMessage is a ScrollingFrame and Content is a TextLabel.
local X = 0
if X < newMessage.Content.TextBounds.X then
	print('Fixing.. X is ' .. X .. ' and will be ' .. newMessage.Content.TextBounds.X)
	local X = newMessage.Content.TextBounds.X
end
print('X = ',X)

OUTPUT

Fixing.. X is 0 and will be 161

X = 0

Why is the value not changing to 161? Is it something simple that I kept forgetting? Thanks! :)

1 answer

✓ Accepted answer

When updating a value, do not put local before it as this will create a new local variable for everything within that section, leaving the previously defined X's value as it is. This is a simple fix, just remove the local on line 5. Your code will then be this:

--NewMessage is a ScrollingFrame and Content is a TextLabel.
local X = 0
if X < newMessage.Content.TextBounds.X then
    print('Fixing.. X is ' .. X .. ' and will be ' .. newMessage.Content.TextBounds.X)
    X = newMessage.Content.TextBounds.X
end
print('X = ',X)

The output should then be:

Fixing.. X is 0 and will be 161

X = 161

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