Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to write these lines more efficiently?

Asked by
Hypgnosis 186
5 years ago

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

Log in to vote
0
Answered by 5 years ago

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

0
Thanks! Hypgnosis 186 — 5y
Ad

Answer this question