Need help with my gamemode.

Okay I’ve been trying for ages and can figure this out. What i need to do is make it so if there’s say 4 people on the server that 3 of them need to be on team survivor and 1 needs to be put on team zombie at the start of a round. so that on my 16 slot server when the round starts 4 random people are put on team zombie and the rest are put on team survivor. Get what I’m saying?

Here’s one way to do it. (Didn’t test it!)

[lua]
//Team 1 being zombie
//Team 2 being survivor
function SetTeams()
local ZCount = 0
while ZCount < 4 do
local ply = table.Random( player.GetAll() )
if ply:Team() != 1 then
ply:SetTeam(1)
ZCount = ZCount + 1
end
end
for _,v in pairs(player.GetAll()) do
if v:Team() != 1 then
v:SetTeam(2)
end
end
end
[/lua]

Thank you i will have to try this tomorrow when i have a lot of people on my server! :smiley: i also wanted to add this.
less then or equal to 4 people = 1 zombie 3 survivors
less then or equal to 8 people = 2 zombie 6 survivors
less then or equal to 12 people = 3 zombie 9 survivors
less then or equal to 16 people = 4 zombie 12 survivors
Thank you and sorry i didn’t mention that. If u want to save me time from trying to figure out how to get how many players are on the server i would much appreciate that.

im guessing something like



local Players = player.GetAll()
 table.Count(Players)


I believe this would work for that. (Also not tested)

[lua]
function SetTeams()
local ratio = 1/4 //Ratio is how many zombies per players (in this case 1 zombie per 4 players)
local ZCount = 0
while ZCount < math.floor(#player.GetAll()*ratio) do
local ply = table.Random( player.GetAll() )
if ply:Team() != 1 then
ply:SetTeam(1)
ZCount = ZCount + 1
end
end
for _,v in pairs(player.GetAll()) do
if v:Team() != 1 then
v:SetTeam(2)
end
end
end
[/lua]

However, if there’s less than 4 people there won’t be any zombies.

Well thank you for replying so fast i am going have so much fun implementing this tomorrow your defiantly going in the credits :smiley:

Thanks! Also in case you wanted to know, using # before any table will return the number of items in it. So like #player.GetAll() will give you the number of players on the server.

Wow. Thank you i would have never found that out. :smiley: trying to come up with a creative way to make at least 1 zombie spawn. :stuck_out_tongue: Also is there an easy way to test this out with out having to beg 16 of my friends to join ?

[editline]16th July 2011[/editline]



function SetTeams()
    local ratio = 1/4  //Ratio is how many zombies per players (in this case 1 zombie per 4 players)
    local ZCount = 0
    while ZCount < math.floor(#player.GetAll()*ratio) do
        local ply = table.Random( player.GetAll() )
        if ply:Team() != 1 then
            ply:SetTeam(1)
            ZCount = ZCount + 1
        end
    end
    for _,v in pairs(player.GetAll()) do
        if v:Team() != 1 and team.NumPlayers(1) >= 1 then
            v:SetTeam(2)
        else
            v:SetTeam(1)
        end
    end
end


Really Stupid but fixes no zombies :stuck_out_tongue:

its 4 am need sleep will try this out tomorrow “later today” lol

Actually, the way I would’ve done it is probably more complicated, haha.

You can use this to test it w/o players (I think):
[lua]
local TestTbl = {}

for i = 1,16 do
TestTbl* = { Team = 0 }
end

function SetTeams()
local ratio = 1/4 //Ratio is how many zombies per players (in this case 1 zombie per 4 players)
local ZCount = 0
while ZCount < math.floor(#TestTbl*ratio) do
local ply = table.Random( TestTbl )
if ply.Team != 1 then
ply.Team = 1
ZCount = ZCount + 1
end
end
for _,v in pairs(TestTbl) do
if v.Team != 1 then
v.Team = 2
else
v.Team = 1
end
end
PrintTable( TestTbl )
end
[/lua]
It’s a little different, but I hope it works.

Also, it’s 4 AM here too, should g2 bed now.

Edit:
Actually the table.HasValue thing won’t work. I took it out.

I’m a little confused. I’m not seeing where its getting the player from TestTbl