Why doesn't my script work?

Sorry I changed the problem because I have a new problem now.

MyTable = {“A”,“B”,“C”,“D”,“E”,“F”,“G”,“F”}

while true do

i = 0

a = math.random(0,7)

while true do

i = i + 1

if i == 6 then break end

if a == i then  script.Parent.Text1.Text = MyTable[i]
	else if a == i+1 then script.Parent.Text2.Text = MyTable[i+1]
			else  if a == i-1 then script.Parent.Text3.Text = MyTable[i-1]
		end
		if script.Parent.Text1.Text and script.Parent.Text2.Text ~= "Label" and
			 script.Parent.Text2.Text and script.Parent.Text3.Text ~= "Label"
				then break end
			end	
end

end
end

This script doesn’t work. I want it to change the text of text labes. The issue is script. It doesn’t give any error. I didn’t look for any help on other websites.

(Script parent is ScreenGui, ScreenGui parent is StarterGui, text labes parent is ScreenGui. Used local script.)

3 Likes

Roblox Studio doesn’t like the quote you have in since it appears to be shown as “ rather than "

Here’s the same code with the proper quotes: Actually don’t use this one, it still times you out regardless.

local MyTable = {"A","B","C","D","E","F"}
while true do
	local i = 0
	local a = math.random(0,7)
		while true do
			wait()
			if i == 6 then i = i - 1
			i = i + 1
			if a == i then script.Parent.Text1.Text = MyTable[i]
				if i == 6 then 
					if script.Parent.Text1.Text and script.Parent.Text2.Text ~= "Label" then
						if script.Parent.Text1.Text and script.Parent.Text3.Text ~= "Label" then
							break
						end
					end
				end
			end
		end
	end
end

Also note: use LocalScripts for anything GUI related.

Fair Warning as well: make sure there is a wait() in the while loop, otherwise, it will crash the client since it’s doing the loop infinitely without cooling down.

1 Like

I’m afraid, truly, this logic is very redundant and spaghetti-like. I’d recommend reviewing this:

I’ve also noticed that your understanding of relational operators is unrefined. For even further reference for that, refer to this:

Overall, here is a refined script that vaguely does the same thing:

-- Get into the habit of making variables local.
local letters = {"A", "B", "C", "D", "E", "F"}

-- We get a random index to use.
local randomLetterIndex = math.random(1, #letters)

-- We get the associated random letter.
local randomLetter = letters[randomLetterIndex]

-- We assign that value to the TextLabel `script.Parent.Text1`'s `Text` field.
script.Parent.Text1.Text = randomLetter

The code not specified is because, logically, it could never run. (In fact, I believe it would always set the value to “F”. Perhaps you can explain what your intended function is?)

1 Like

Thanks for script advice but I want to know where did I make mistake? What was it?

Please let me know.

1 Like

It would help if you could either tell or confirm what the intended purpose of this code was.

But yeah, for sure! I can tell you the issues:

Invalid String Syntax

MyTable = {“A”, “B”, “C”, “D”, “E”, “F”, “G”, “F”}

This is syntactically inaccurate. For short strings, they must be enclosed by either single quotes (') or double quotes ("). You seem to be using the Unicode Left and Right double quotes ( and ), which are not the same, or correct. It should be written like so:

MyTable = {"A", "B", "C", "D", "E", "F"}

Misuse of Logical Operators

Then your understanding of logical operators in Lua (and, or, and not) follows the semantics more of common language as opposed to logical operation. Here’s a snippet from your code:

 if script.Parent.Text1.Text and script.Parent.Text2.Text ~= "Label"

I’m assuming you’re trying to semantically program “if the Text for script.parent.Text1 and script.Parent.Text2 both are not equal to ‘Label’”.

The computer, and most other programmers who are used to reading logic, well, logically reads this condition as “if the Text for script.Parent.Text1 exists and the Text for script.Parent.Text2 is not ‘Label’”. It is very different.

Unreachable Code / Functionless Code

You have several if statements that either will never occur or nothing happens if those conditions are met. These can and should be omitted. Definitely don’t get in the habit of creating these situations.

Note

There’s also a few issues regarding not using local, for instead of while, et cetera; but considering the question was getting it to work then these can be waived for now.