Advertisement
Guest User

GameTicker

a guest
May 21st, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.30 KB | None | 0 0
  1. -- Variables for message players and victor
  2. local oldMessage = ''
  3. local minPlayers = 2
  4. local alivePlayers = 0
  5. local victor = nil
  6.  
  7. local negCorner = workspace.NegXNegZ.Position
  8. local posCorner = workspace.PosXPosZ.Position
  9. local ground = workspace.Center.Position
  10.  
  11. -- Main state variable that starts at lobby
  12. local state = 'Lobby'
  13.  
  14. -- Variables for the different state changes
  15. local stateLOBBY = 'Lobby'
  16. local statePLAYING = 'Playing'
  17. local stateROUNDEND = 'RoundEnd'
  18.  
  19. -- Random value that gives our game randomization
  20. math.randomseed(tick())
  21.  
  22. -- Announce function that replaces the message and sends to clients
  23. function Announce(message)
  24.     if oldMessage ~= message then
  25.         oldMessage = message
  26.         game.ReplicatedStorage.Announce:FireAllClients(message)
  27.     end
  28. end
  29.  
  30. -- Add player function that keeps track of current players
  31. function addPlayer(player)
  32.     local playing = Instance.new('BoolValue', player)
  33.     playing.Value = false
  34.     playing.Name = 'Playing'
  35.    
  36.     -- Local function within addPlayer function to remove active players when they die or leave
  37.     local function removeCharacter()
  38.         if (player.Playing.Value == true) then
  39.             alivePlayers = alivePlayers - 1
  40.             player.Playing.Value = false
  41.         end
  42.     end
  43.     player.CharacterRemoving:Connect(removeCharacter)
  44.    
  45. end
  46. game:GetService('Players').PlayerAdded:Connect(addPlayer)
  47.  
  48.  
  49. -- Function that runs the lobby code
  50. function Lobby()
  51.     Announce('Waiting for more players...')
  52.     wait(5)
  53.    
  54.     -- Check if the num of players alive is more or equal to 2
  55.     if table.getn(game.Players:GetPlayers()) >= minPlayers then
  56.         -- Countdown for 5s and then start game
  57.         for i =5, 0, -1 do
  58.             Announce('Round starts in '.. i)
  59.             wait(1)
  60.         end
  61.         startGame()
  62.     else
  63.         Announce('Waiting for more players...')
  64.     end
  65.    
  66. end
  67.  
  68. -- Function that runs the start game code
  69. function startGame()
  70.     -- Spawn a weapon for every spawn location we have
  71.     for i, spawner in ipairs(workspace.WeaponSpawners:GetChildren()) do
  72.         spawner.Spawn:Fire()
  73.     end
  74.    
  75.     -- Spawn a vehicle for every spawn location we have
  76.     for i, spawner in ipairs(workspace.VehicleSpawners:GetChildren()) do
  77.         spawner.Spawn:Fire()
  78.     end
  79.    
  80.     -- Count active players in game and set playing value to true
  81.     for i, player in ipairs(game.Players:GetPlayers()) do
  82.         alivePlayers = alivePlayers + 1
  83.         player.Playing.Value = true
  84.        
  85.         -- Get teleport location and then teleport the player
  86.         local teleLoc = CFrame.new(math.random(negCorner.X + 10, posCorner.X - 10),ground.Y + 100,math.random(negCorner.Z+10, posCorner.Z-10))
  87.         player.Character.HumanoidRootPart.CFrame = teleLoc
  88.        
  89.         -- Clone the sword and give it to the player
  90.         local swordClone = game.ServerStorage:FindFirstChild('Laser Sword'):Clone()
  91.         swordClone.Parent = player:WaitForChild('Backpack')
  92.     end
  93.     state = statePLAYING
  94. end
  95.  
  96. -- Function that runs the playing code
  97. function Playing()
  98.     -- Announce the num of players still alive
  99.     Announce(alivePlayers ..' still alive!')
  100.     -- When there is one player left go to round-end state
  101.     if (alivePlayers <= 1) then
  102.         state = stateROUNDEND
  103.     end
  104. end
  105.  
  106. -- Function that runs the round end code
  107. function roundEnd()
  108.     -- Check which player won
  109.     for i, player in ipairs(game.Players:GetPlayers()) do
  110.         if (player.Playing.Value == true) then
  111.             victor = player
  112.             break
  113.         end
  114.     end
  115.    
  116.     -- Celebrate winner or tie
  117.     if victor then
  118.         victor.Character.HumanoidRootPart.CFrame =  workspace.SpawnBase.VictoryPedestal.CFrame + Vector3.new(0,3,0)
  119.         Announce(victor.Name..'wins!')
  120.        
  121.         wait(10)
  122.         if victor then
  123.             victor:LoadCharacter()
  124.         end
  125.     else
  126.         Announce('Tie?!')
  127.     end
  128.     -- Wait a bit and then reset victor
  129.     wait(1)
  130.     victor = nil
  131.    
  132.     -- Change state to lobby
  133.     state = stateLOBBY
  134.    
  135.     -- For loop that destroys weapon at the end of every round
  136.     for i, weapon in ipairs(workspace.Weapons:GetChildren()) do
  137.         weapon:Destroy()
  138.     end
  139.    
  140.     -- For loop that destroys vehicle at the end of every round
  141.     for i, vehicle in ipairs(workspace.Vehicles:GetChildren()) do
  142.         vehicle:Destroy()
  143.     end
  144. end
  145.  
  146. -- Create initial wait fo that the game has time to load
  147. wait(10)
  148.  
  149. -- Infinite game while loop that changes depending on state value
  150. while(true) do
  151.     wait(1)
  152.    
  153.     if (state == stateLOBBY) then
  154.         Lobby()
  155.     end
  156.    
  157.     if (state == statePLAYING) then
  158.         Playing()
  159.     end
  160.    
  161.     if (state == stateROUNDEND) then
  162.         roundEnd()
  163.     end
  164. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement