I recently started my very first Garry's Mod gamemode, and I'm kinda stuck. How can I make a simple round system? I've made it so when a player dies, he is set to a spectator and can free roam, but how can I make it so when there is 1 player left, the round restarts?
Now, I'm not asking for someone to write me a whole code, I just need a nudge in the correct direction.
Also, I've got about 5 hours of Lua experience, so I'm about as new as they come. Thanks!
Hi there, welcome to Facepunch. :v:
Just keep changing people to the spectator team when they die, count the number of players left in the normal player team with [b][url=http://wiki.garrysmod.com/page/team/NumPlayers][img]http://wiki.garrysmod.com/favicon.ico[/img] team.NumPlayers()[/url][/b] and trigger your round end when there's 1 player left.
[QUOTE=Luni;42331755]Hi there, welcome to Facepunch. :v:
Just keep changing people to the spectator team when they die, count the number of players left in the normal player team with [b][url=http://wiki.garrysmod.com/page/team/NumPlayers][img]http://wiki.garrysmod.com/favicon.ico[/img] team.NumPlayers()[/url][/b] and trigger your round end when there's 1 player left.[/QUOTE]
When I try that, it just still doesn't work! I have it set this way in init.lua. (team 2 is player, 1 is spectator)
[code]if team.NumPlayers( 2 ) == 1 then
game.CleanUpMap()
ply:SetTeam( 1 )
ply:Spawn()
end[/code]
Thanks for helping me so far!
I'm gonna assume that your code looks like
[lua]
function GM:PlayerDeath(ply, inflictor, attacker)
if team.NumPlayers(2) == 1 then
game.CleanUpMap()
ply:SetTeam(1)
ply:Spawn()
end
end
[/lua]
You need to move everyone to team 2, not just the player who died:
[lua]
function GM:PlayerDeath(ply, inflictor, attacker)
-- just in case i guess?
if ply:Team() == 1 then return end
-- would there be 1 person left if we changed him?
if team.NumPlayers(2) <= 2 then
-- end of round: set everyone back to normal
game.CleanUpMap()
for k, v in pairs(player.GetAll()) do
ply:SetTeam(2)
ply:Spawn()
end
return
else
ply:SetTeam(1)
end
end[/lua]
[QUOTE=Luni;42332466]I'm gonna assume that your code looks like
[lua]
function GM:PlayerDeath(ply, inflictor, attacker)
if team.NumPlayers(2) == 1 then
game.CleanUpMap()
ply:SetTeam(1)
ply:Spawn()
end
end
[/lua]
You need to move everyone to team 2, not just the player who died:
[lua]
function GM:PlayerDeath(ply, inflictor, attacker)
-- just in case i guess?
if ply:Team() == 1 then return end
-- would there be 1 person left if we changed him?
if team.NumPlayers(2) <= 2 then
-- end of round: set everyone back to normal
game.CleanUpMap()
for k, v in pairs(player.GetAll()) do
ply:SetTeam(2)
ply:Spawn()
end
return
else
ply:SetTeam(1)
end
end[/lua][/QUOTE]
Almost there, but it only respawns the last person that died. Thank you so much for helping this far! Also, sorry to be so needy, but how could I print "<PLAYER> has won the round! Restarting..." when the round is over?
It should look like this:
[CODE]function GM:PlayerDeath(ply, inflictor, attacker)
-- just in case i guess?
if ply:Team() == 1 then return end
-- would there be 1 person left if we changed him?
if team.NumPlayers(2) <= 2 then
-- end of round: set everyone back to normal
game.CleanUpMap()
for k, v in pairs(player.GetAll()) do
v:SetTeam(2)
v:Spawn()
end
return
else
ply:SetTeam(1)
end
end[/CODE]
[QUOTE=BoastingToast;42341561]Almost there, but it only respawns the last person that died. Thank you so much for helping this far! Also, sorry to be so needy, but how could I print "<PLAYER> has won the round! Restarting..." when the round is over?[/QUOTE]
Also, about the printing. When you figure out which player is the last alive, put
for k, v in pairs(player.GetAll()) do
v:ChatPrint(attacker.." has won the round! \nRestarting...")
end
Thanks for the help guys, got it working!
Sorry, you need to Log In to post a reply to this thread.