Want to repeat/loop function

Okay, so I have been working on a name-changing script (clientside) as practice for Lua. I am a bit stuck on one part though, I am trying to make the script repeat itself once it finishes printing all of the names. For example, if I have 5 names and the 5th name is printed, I want it to go back and print names 1-5 again, and keep doing so.

Here’s an example of what I have, except with two names only.


function NameChange()
	timer.Create("PlayerName1", 1, 1, NameOne)
	timer.Create("PlayerName2", 2, 1, NameTwo)

end

names={}
names[1]="DudeOne"
names[2]="GuyTwo"


function NameOne ()
	LocalPlayer():PrintMessage(HUD_PRINTTALK, "Name #1: 1 second. "..names[1])
end

function NameTwo ()
	LocalPlayer():PrintMessage(HUD_PRINTTALK, "Name #2: 2 seconds. "..names[2])
end

NameChange()


If you have any questions about the code, or need to know anything else, please feel free to ask and I would be glad to respond.



hook.Add("Think", "dlaldalwdaowiraldawld", function()

function Namo(r)
NameChange("john552")
end

Namo("new name")

function NameChange()
Namo()
end


try this

This doesn’t seem to be working.



function Namo()
RunConsoleCommand("say", "/rpname", "john552")
end

timer.Create("DAWD", 5, 0, function()
Namo()
end )


Are you trying to cycle through a list of names with a delay of 1 second?

Yes, but then repeat it.

try what i said

hacky, but might work. Just replace print with what you want to do with the name.

[lua]
names = {}
names[1] = “bob”
names[2] = “ted”

function NameCycle(count)
LocalPlayer().NameCycle = count or LocalPlayer().NameCycle or 1

local NextCount = LocalPlayer().NameCycle + 1
if NextCount > #names then
	NextCount = 1
end
timer.Create("NameCycling",1,1,function()
	print(names[count])
	NameCycle(NextCount)
end)

end

NameCycle()
[/lua]

Edit: missed a closing bracket

Very sloppy edit: and it won’t work

Thanks man, it works! I am just curious though, do you know why it printed “nil” when I first ran the code?

Because I forgot to declare the variable count on the first run…
[lua]
names = {}
names[1] = “bob”
names[2] = “ted”

function NameCycle(count)
count = count or 1

local NextCount = count + 1
if NextCount > #names then
	NextCount = 1
end
timer.Create("NameCycling",1,1,function()
	print(names[count])
	NameCycle(NextCount)
end)

end

NameCycle()
[/lua]

Ah, okay. Thanks for all the help!