• (noob) How to get "ply" in a custom function?
    5 replies, posted
I have this piece of Lua code [CODE]function StartRace() PrintMessage( HUD_PRINTTALK, "The race has started!") if ply:Team() == 2 then ply:SetTeam( 1 ) ply:Spawn() ply:PrintMessage( HUD_PRINTTALK, "You're in team 1! " ) end end[/CODE] But obviously it doesn't work because it has nowhere to pull the ply: var from. I'm very new to Lua so I'm wondering how I can make it so that when function StartRace is called all players on Team 2 change to Team 1? Thanks for your help. EDIT: Here's the full countdown timer code if needed [CODE]function StartRace() PrintMessage( HUD_PRINTTALK, "The race has started!") if ply:Team() == 2 then ply:SetTeam( 1 ) ply:Spawn() ply:PrintMessage( HUD_PRINTTALK, "You're in team 1! " ) end end function CountdownStartRace() timeleft = timer.RepsLeft( "RaceStartCountdown" ) if timeleft != 0 then PrintMessage( HUD_PRINTTALK, "Next race starts in "..timeleft ) end if timeleft == 0 then StartRace() end end function CreateCountdownStartRace() racedelay = 6 timer.Create( "RaceStartCountdown", 1, racedelay, CountdownStartRace ) end hook.Add( "Initialize", "Timer Example", CreateCountdownStartRace )[/CODE]
Try using this( [url]http://wiki.garrysmod.com/page/team/GetPlayers[/url] ) and looping through the table of players that it returns.
[QUOTE=InfernusN;49293135]Try using this( [url]http://wiki.garrysmod.com/page/team/GetPlayers[/url] ) and looping through the table of players that it returns.[/QUOTE] I tried using that but I have no idea how to correctly put it in my code. Thanks for pointing me in the right direction, but I literally started using Lua an hour ago, would you be so kind to show me how that's done?
Give me 10 min and I'll be in the train so I can help you.
[QUOTE=Yonnick19;49293151]I tried using that but I have no idea how to correctly put it in my code. Thanks for pointing me in the right direction, but I literally started using LUA an hour ago, would you be so kind to show me how that's done?[/QUOTE] [LUA]for k, ply in pairs( team.GetPlayers( 2 ) ) do -- Your code here end[/LUA] This goes through everything in the table that team.GetPlayers( 2 ) returns, with k corresponding to its key and ply corresponding to its value. You could then write inside the middle of the loop what you want to happen to all the players in team 2. Also it's Lua, not LUA.
[QUOTE=InfernusN;49293169][LUA]for k, ply in pairs( team.GetPlayers( 2 ) ) do -- Your code here end[/LUA] This goes through everything in the table that team.GetPlayers( 2 ) returns, with k corresponding to its key and ply corresponding to its value. You could then write inside the middle of the loop what you want to happen to all the players in team 2. Also it's Lua, not LUA.[/QUOTE] That worked perfectly, thank you very much for the explanation, this helped me along a lot!
Sorry, you need to Log In to post a reply to this thread.