--Code by Zwataketa-- COM_AddCommand('autospec_on', function(player, arg) if arg == nil then CONS_Printf(player, 'Auto Spectate is '+player.autoSpec+'. Default is false.') else if arg == 'true' player.autoSpec = true else if arg == 'false' player.autoSpec = false else CONS_Printf(player, 'Auto Spectate is '+player.autoSpec+'. Default is false.') end end end end) COM_AddCommand('afktoggle', function(player) if autoSpec == true player.autoSpec = false else player.autoSpec = true end end) COM_AddCommand('autospec_timer', function(player, arg) if arg != nil then local isNumber = (type(arg[1]) == "number") if isNumber == false then if tonumber(arg) > 30 arg = 30 end if tonumber(arg) < 5 arg = 5 end player.autoSpec_timer = arg CONS_Printf(player, 'Auto Spectate timer set to '+player.autoSpec_timer+'.') else CONS_Printf(player, 'Auto Spectate timer is set to '+player.autoSpec_timer+'. Default is 10.') end else CONS_Printf(player, 'Auto Spectate timer is set to '+player.autoSpec_timer+'. Default is 10.') end end) local function getRandomPlayer() local player_table = {} -- collected players for player in players.iterate do if(player != consoleplayer and player != displayplayer) -- exclude self and currently viewed player table.insert(player_table, player) end end if #player_table == 0 then -- if no players exist return end return player_table[P_RandomRange(1,#player_table)] end local function spectateRand() if consoleplayer.autoSpec then displayplayer = getRandomPlayer() end end addHook("PlayerThink", function(player) if (player.autoSpec == nil) then player.autoSpec = false end if (player.autoSpec_timer == nil) then player.autoSpec_timer = 10 end if (leveltime % (35*player.autoSpec_timer)) == 0 then spectateRand() end end) addHook("PlayerSpawn", function(player) if (player.autoSpec == nil) then player.autoSpec = false end if (player.autoSpec_timer == nil) then player.autoSpec_timer = 10 end spectateRand() end) --print(getPlayerCount()) -- print the amount of players in the server