• Winner Selection Code Selects the last person who got killed.
    2 replies, posted
I made a script for my gamemode which should find the winner and set the winner variable to the winners nickname. But instead it selects the LAST person who got killed. (A person re spawns and goes into the spectator team. So this script is finding who is the last person on the players team) [CODE]function GM:PostPlayerDeath(ply) players_alive = players_alive - 1 print (players_alive) print "A Player Has Died." if players_alive == 1 then winner = "(ERROR WINNER NOT FOUND)" players_alive = player.GetCount() for k, v in pairs(player.GetAll()) do if (v:Team() == 1) then winner = v:Nick() end end timer.Simple(1, NewRound) end end[/CODE] Also here is the new round Function [CODE]function NewRound() for k, v in pairs(player.GetAll()) do --Respawns Everyone and ends the round v:SetTeam(PLAYERS) v:Spawn() v:ChatPrint("The round has ended!") v:ChatPrint("The winner was..." .. winner) end end[/CODE] EXAMPLE ON THIS BUG: CH4NCE KILLS BOT62 (The last alive person besides CH4NCE gets killed so ch4nce should be the winner) [QUOTE=Output]The round has ended! The winner was...Bot62[/QUOTE] For some reason bot62 wins. (There are no lua errors)
So there's no point assigning the winner every time a player dies. Instead, when you start a new round, before you respawn everyone and set their team back, work out the winner there. This code isn't tested. [lua] function NewRound() local winner = "Nobody" if (team.NumPlayers(1) == 1) then winner = team.GetPlayers(1)[1] end for k, v in pairs(player.GetAll()) do --Respawns Everyone and ends the round v:SetTeam(PLAYERS) v:Spawn() v:ChatPrint("The round has ended!") v:ChatPrint(winner .. " won this round!") end end [/lua]
[QUOTE=BadgerCode;52436828]So there's no point assigning the winner every time a player dies. Instead, when you start a new round, before you respawn everyone and set their team back, work out the winner there. This code isn't tested. [lua] function NewRound() local winner = "Nobody" if (team.NumPlayers(1) == 1) then winner = team.GetPlayers(1)[1] end for k, v in pairs(player.GetAll()) do --Respawns Everyone and ends the round v:SetTeam(PLAYERS) v:Spawn() v:ChatPrint("The round has ended!") v:ChatPrint(winner .. " won this round!") end end [/lua][/QUOTE] Oh thanks! Just moving it into 1 function seems to have worked! Thank you!
Sorry, you need to Log In to post a reply to this thread.