• Want to repeat/loop function
    11 replies, posted
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. [CODE]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() [/CODE] 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.
[code] hook.Add("Think", "dlaldalwdaowiraldawld", function() function Namo(r) NameChange("john552") end Namo("new name") function NameChange() Namo() end [/code] try this
[QUOTE=john552;45265271][code] hook.Add("Think", "dlaldalwdaowiraldawld", function() function Namo(r) NameChange("john552") end Namo("new name") function NameChange() Namo() end [/code] try this[/QUOTE] This doesn't seem to be working.
[QUOTE=SteamBuck;45265350]This doesn't seem to be working.[/QUOTE] [code] function Namo() RunConsoleCommand("say", "/rpname", "john552") end timer.Create("DAWD", 5, 0, function() Namo() end ) [/code]
Are you trying to cycle through a list of names with a delay of 1 second?
[QUOTE=Blasteh;45265422]Are you trying to cycle through a list of names with a delay of 1 second?[/QUOTE] 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
[QUOTE=Blasteh;45265533]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[/QUOTE] Very sloppy edit: and it won't work
[QUOTE=Blasteh;45265533]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[/QUOTE] 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]
[QUOTE=Blasteh;45265640]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][/QUOTE] Ah, okay. Thanks for all the help!
Sorry, you need to Log In to post a reply to this thread.