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

How to write these lines more efficiently?

I've written the following code to continuously loop sting values, repeating in the same order infinitely. I was just curious if there was any way to write lines 6 through 13 more effectively?

local message = script.Parent

local myArray = {"Hello, there.", "General Kenobi!", "I am the Senate.", "Not... yet."}

while true do
	message.Name = myArray[1]
	wait(3)
	message.Name = myArray[2]
	wait(3)
	message.Name = myArray[3]
	wait(3)
	message.Name = myArray[4]
	wait(3)
end

1 answer

✓ Accepted answer

you could try to do a for loop:

local message = script.Parent

local myArray = {"Hello, there.", "General Kenobi!", "I am the Senate.", "Not... yet."}

while true do
	for i = 1,  #myArray do
		message.Name = myArray[i]
		wait(3)
	end
end

in my opinion, for loops work better for longer tables, also you could do

local message = script.Parent

local myArray = {"Hello, there.", "General Kenobi!", "I am the Senate.", "Not... yet."}

while true do
	for _,v in pairs(myArray) do
		message.Name = v
		wait(3)
	end
end

the latter also works for dictionaries

1 comment

0

Thanks!

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