• Run a console command run if there is + 4 players and another if there is under 4 players.
    18 replies, posted
How would i make a console command run if there is over 4 players and another if there is under 4 players? Like when there are 4 players in the server it runs Manhack_Start And when someone leaves and there is under 4 players it runs Manhack_Stop. Thank you.
What is your definition of "run"? Is it a entity that we'll fire a input on? Or is it a global variable we'll turn on and off?
Something like this, perhaps? [lua]local bManhack = false; hook.Add("PlayerConnect","ManhackStart",function() if #player.GetAll() >= 4 and not bManhack then game.ConsoleCommand("Manhack_Start"); bManhack = true; end end) hook.Add("PlayerDisconnect","ManhackEnd",function() if #player.GetAll() < 4 and bManhack then game.ConsoleCommand("Manhack_End"); bManhack = false; end end)[/lua]
Entoros is correct. #player.GetAll()
[QUOTE=Entoros;24241337]Something like this, perhaps? [lua]local bManhack = false; hook.Add("PlayerConnect","ManhackStart",function() if #player.GetAll() >= 4 and not bManhack then game.ConsoleCommand("Manhack_Start"); bManhack = true; end end) hook.Add("PlayerDisconnect","ManhackEnd",function() if #player.GetAll() < 4 and bManhack then game.ConsoleCommand("Manhack_End"); bManhack = false; end end)[/lua][/QUOTE] Thank you. But i still got a problem :/ Well in the first place it didten work. I changed game.ConsoleCommand to RunConsoleCommand which then worked but only on more then 4 players. When i then kick the players and it goes under 4 it still keeps spawning the manhacks. Off-Topic: Wizey i sended you a PM would you mind looking at it? ;)
[QUOTE=Entoros;24241337]Something like this, perhaps? [lua]local bManhack = false; hook.Add("PlayerConnect","ManhackStart",function() if #player.GetAll() >= 4 and not bManhack then game.ConsoleCommand("Manhack_Start\n"); bManhack = true; end end) hook.Add("PlayerDisconnect","ManhackEnd",function() if #player.GetAll() < 4 and bManhack then game.ConsoleCommand("Manhack_End\n"); bManhack = false; end end)[/lua][/QUOTE] Fixed added "\n" in game.ConsoleCommand. [QUOTE] The command needs to be ended with a \n to be run, as shown in the examples. [/QUOTE]
[QUOTE=Cubar;24242893]Fixed added "\n" in game.ConsoleCommand.[/QUOTE] Agein it worked in the begining but when it shuld run Manhack_Stop nothing happend and it keep'd spawning manhacks.
Show your code would help.
[QUOTE=Cubar;24244105]Show your code would help.[/QUOTE] [lua] local bManhack = false; hook.Add("PlayerConnect","ManhackStart",function() if #player.GetAll() >= 4 and not bManhack then game.ConsoleCommand("Manhack_Start\n"); bManhack = true; end end) hook.Add("PlayerDisconnect","ManhackStop",function() if #player.GetAll() < 4 and bManhack then game.ConsoleCommand("Manhack_Stop\n"); bManhack = false; end end)[/lua] It works when there is +4 players but when there is -4 players it keep spawning manhacks.
[QUOTE=Crap-Head;24258220][lua] local bManhack = false; hook.Add("PlayerConnect","ManhackStart",function() if #player.GetAll() >= 4 and not bManhack then game.ConsoleCommand("Manhack_Start\n"); bManhack = true; end end) hook.Add("PlayerDisconnect","ManhackStop",function() if #player.GetAll() < 4 and bManhack then game.ConsoleCommand("Manhack_Stop\n"); bManhack = false; end end)[/lua] It works when there is +4 players but when there is -4 players it keep spawning manhacks.[/QUOTE] I mean the coding for ManhackStart and ManhackStop.
[QUOTE=Cubar;24261516]I mean the coding for ManhackStart and ManhackStop.[/QUOTE] Heres the code for the Man Hack spawner: [lua] SpawnPos = { -- Tables for spawn pos's Vector( -90, 298, -84 ), -- Pos Vector( 222, 222, 222 ), -- Another pos Vector( 333, 333, 333 ) -- Third pos Make other if needed. } function SpawnThem() Interval = 5 -- Amount of time between manhack spawn. Limit = 0 -- 0 is infinite. timer.Create( "Manhack_Timer", Interval, Limit, function() -- Create a timer. local hack = ents.Create( "npc_manhack" ) -- Its the manhack! hack:Spawn() hack:SetPos(table.Random(SpawnPos)) -- Set the positions of them using the table from before end ) end function StopThem() timer.Destroy( "Manhack_Timer" ) -- If you think there are enough players on the server stop the manhacks from being spawned. end concommand.Add("Manhack_Start", SpawnThem) -- Typing "Manhack_Start" in console will start the timer concommand.Add("Manhack_Stop", StopThem) -- Typing "Manhack_Stop" in console will stop the timer [/lua]
Hmm. Do something for me, because I'm not entirely sure where PlayerDisconnect gets called in the timeline of things. In the PlayerDisconnect hook, just at the top of the function, put [code]print("PlayerDisconnect counts " .. #player.GetAll() .. " players.")[/code] and tell me what that prints when someone disconnects. Then tell me how many people were in the server after that player disconnected.
[QUOTE=Entoros;24264130]Hmm. Do something for me, because I'm not entirely sure where PlayerDisconnect gets called in the timeline of things. In the PlayerDisconnect hook, just at the top of the function, put [code]print("PlayerDisconnect counts " .. #player.GetAll() .. " players.")[/code] and tell me what that prints when someone disconnects. Then tell me how many people were in the server after that player disconnected.[/QUOTE] Nothing :/ It prints nothing. I joined the server. Added 4 bots and kicked 1 after 1 but nothing happend. [lua] hook.Add("PlayerDisconnect","ManhackStop",function() print("PlayerDisconnect counts " .. #player.GetAll() .. " players.") if #player.GetAll() < 4 and bManhack then game.ConsoleCommand("Manhack_Stop\n"); bManhack = false; end end) [/lua] Thats how the PlayerDisconnect hook looks.
hook it to [B][URL="http://wiki.garrysmod.com/?title=Gamemode.PlayerDisconnected"]Gamemode.PlayerDisconnected [IMG]http://wiki.garrysmod.com/favicon.ico[/IMG][/URL][/B], Player.Disconnect does not exist.
Either way, Player.Disconnect would not have worked.
[QUOTE=Crap-Head;24288173][lua]hook.Add("PlayerDisconnect","ManhackStop",function() print("PlayerDisconnect counts " .. #player.GetAll() .. " players.") if #player.GetAll() < 4 and bManhack then game.ConsoleCommand("Manhack_Stop\n"); bManhack = false; end end)[/lua][/QUOTE] I mean PlayerDisconnect, sorry.
Use [b][url=wiki.garrysmod.com/?title=Gamemode.EntityRemoved]Gamemode.EntityRemoved [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b]. It is called every time an entity is removed. If the entity is a player, that means that a player has just disconnected, it's as easy as that. :science: [editline]05:19PM[/editline] [lua] hook.Add("EntityRemoved","ManhackStop",function(ent) if ent:IsPlayer() and #player.GetAll() < 4 and bManhack then RunConsoleCommand("Manhack_Stop"); bManhack = false; end end) [/lua] [editline]05:20PM[/editline] Why do you need console commands anyway? Just call SpawnThem() and StopThem() whenever you want to start or stop spawning manhacks.
Sorry, you need to Log In to post a reply to this thread.