• Print

Author Topic: Repetition  (Read 4234 times)

0 Members and 1 Guest are viewing this topic.

Offline Zmaster

  • Full Member
  • ***
  • Posts: 235
  • Karma: 25
Repetition
« on: August 02, 2014, 09:58:51 pm »
Thanks to you guys I was able to create a script that gives the winning team of a DeathRun round 25 points (for PointShop)

I added another part to that script that slays all players who are alive to fix a small bug

The problem is, the message that says "The round has ended! All living players have been slain." repeats two (or more) times when it's printed to the chat, and I'm not sure why

I tried to add a timer to make sure it only calls itself once, but it still prints two+ times

Here's the code

Code: Lua
  1. if SERVER then
  2.         hook.Add( "OnRoundSet", "DR Money", function( round, winner )
  3.                 if round == ROUND_ENDING then
  4.                         for k, v in next, player.GetAll() do
  5.                                 if not v:Team() == winner then
  6.                                         continue
  7.                                 end
  8.                                 v:PS_GivePoints( 25 )
  9.                         end
  10.                         local name = team.GetName( winner )
  11.                         PrintMessage( HUD_PRINTTALK, "All players on the " .. name .. " team were given 25 points for winning the round!" )
  12.                        
  13.                         timer.Create( "TimerRoundEndSlay" , 1, 1, SlayAtRoundEnd )
  14.                 end
  15.         end )
  16. end
  17.  
  18. function SlayAtRoundEnd()
  19.         for k, v in pairs( player.GetAll() ) do
  20.                 if ( v:Alive() ) then
  21.                         v:Kill()
  22.                 end
  23.                 PrintMessage( HUD_PRINTTALK, "The round has ended! All living players have been slain." )
  24.         end
  25. end

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Repetition
« Reply #1 on: August 02, 2014, 10:34:21 pm »
Take the printmessage with that text outside of the for loop.
It will print for each time the GetAll has a player object.
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline Zmaster

  • Full Member
  • ***
  • Posts: 235
  • Karma: 25
Re: Repetition
« Reply #2 on: August 02, 2014, 11:01:57 pm »
Alrighty, changed the function:
Code: Lua
  1. function SlayAtRoundEnd()
  2.         for k, v in pairs( player.GetAll() ) do
  3.                 if ( v:Alive() ) then
  4.                         v:Kill()
  5.                 end
  6.         end
  7.         PrintMessage( HUD_PRINTTALK, "The round has ended! All living players have been slain." )
  8. end

aaaaaaaaaaand it works perfectly now. Thanks, JamminR!

  • Print