Advertisement
Guest User

Untitled

a guest
Feb 26th, 2020
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.38 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
  12. local state = "Lobby"
  13. --variables for the different state changes
  14. local stateLOBBY = "Lobby"
  15. local statePLAYING = "Playing"
  16. local stateROUNDEND = "RoundEnd"
  17.  
  18. --random function that gives our game randomization
  19. math.randomseed(tick())
  20.  
  21. --announce function that sends message to clients
  22. function Announce(message)
  23.     if oldMessage ~= message then
  24.         oldMessage = message
  25.         game.ReplicatedStorage.Announce:FireAllClients(message)
  26.     end
  27. end
  28.  
  29. --add player function that keeps track of current players
  30. function addPlayer(player)
  31.     local playing = Instance.new("BoolValue", player)
  32.     playing.Value = false
  33.     playing.Name = "Playing"
  34.     --remove character function that removes active players when they die or leave
  35.     local function removeCharacter()
  36.         if(player.Playing.Value == true) then
  37.             alivePlayers = alivePlayers - 1
  38.             player.Playing.Value = false
  39.         end
  40.     end
  41.     player.CharacterRemoving:Connect(removeCharacter)
  42. end
  43. game:GetService("Players").PlayerAdded:Connect(addPlayer)
  44.  
  45. function Lobby()
  46.     Announce("Waiting for more players...")
  47.     wait(5)
  48.    
  49.     --if statement that checks if we have enough players
  50.     if table.getn(game.Players:GetPlayers()) >= minPlayers then
  51.         for i = 5, 0, -1 do
  52.             Announce("Round starts in ".. i)
  53.             wait(1)
  54.         end
  55.         startGame()
  56.     else
  57.         Announce("Waiting for more players...")
  58.     end
  59. end
  60.  
  61. function startGame()
  62.     --spawn a weapon for every spawn location we have
  63.     for i, spawner in ipairs(workspace.WeaponSpawners:GetChildren()) do
  64.         spawner.Spawn:Fire()
  65.     end
  66.    
  67.     --count active players and mark them as active
  68.     for i, player in ipairs(game.Players:GetPlayers()) do
  69.         alivePlayers = alivePlayers + 1
  70.         player.Playing.Value = true
  71.        
  72.         local teleLoc = CFrame.new(math.random(negCorner.X + 10, posCorner.X - 10), ground.Y + 100, math.random(negCorner.Z + 10, posCorner.Z - 10))
  73.         player.Character.HumanoidRootPart.CFrame = teleLoc
  74.     end
  75.    
  76.     state = statePLAYING
  77. end
  78.  
  79. function Playing()
  80.     Announce(alivePlayers.." still alive!")
  81.    
  82.     --go to roundend when there is one player left
  83.     if(alivePlayers <= 1) then
  84.         state = stateROUNDEND
  85.     end
  86. end
  87.  
  88. function roundEnd()
  89.     --for loop that looks for the player still alive (winnder!)
  90.     for i, player in ipairs(game.Players:GetPlayers()) do
  91.         if(player.Playing.Value == true) then
  92.             victor = player
  93.             break
  94.         end
  95.     end
  96.    
  97.     --announce the victor or a tie
  98.     if victor then
  99.         Announce(victor.Name.." wins!")
  100.        
  101.         wait(10)
  102.         if victor then
  103.             victor:LoadCharacter()
  104.         end
  105.     else
  106.         Announce("Tie?!")
  107.     end
  108.    
  109.     --wait a bit then reset veictor
  110.     wait(1)
  111.     victor = nil
  112.    
  113.     --change state to lobby
  114.     state = stateLOBBY
  115.    
  116.     for i, weapon in ipairs(workspace.Weapons:GetChildren()) do
  117.         weapon:Destroy()
  118.     end
  119. end
  120.  
  121. --short wait when we start the game so everything can initialize
  122. wait(10)
  123.  
  124. --infinite game loop that manages the state of our game
  125. while(true) do
  126.     wait(1)
  127.    
  128.     if(state == stateLOBBY) then
  129.         Lobby()
  130.     end
  131.    
  132.     if(state == statePLAYING) then
  133.         Playing()
  134.     end
  135.    
  136.     if(state == stateROUNDEND) then
  137.         roundEnd()
  138.     end
  139. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement