Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Variables for message players and victor
- local oldMessage = ''
- local minPlayers = 2
- local alivePlayers = 0
- local victor = nil
- local negCorner = workspace.NegXNegZ.Position
- local posCorner = workspace.PosXPosZ.Position
- local ground = workspace.Center.Position
- -- Main state variable that starts at lobby
- local state = 'Lobby'
- -- Variables for the different state changes
- local stateLOBBY = 'Lobby'
- local statePLAYING = 'Playing'
- local stateROUNDEND = 'RoundEnd'
- -- Random value that gives our game randomization
- math.randomseed(tick())
- -- Announce function that replaces the message and sends to clients
- function Announce(message)
- if oldMessage ~= message then
- oldMessage = message
- game.ReplicatedStorage.Announce:FireAllClients(message)
- end
- end
- -- Add player function that keeps track of current players
- function addPlayer(player)
- local playing = Instance.new('BoolValue', player)
- playing.Value = false
- playing.Name = 'Playing'
- -- Local function within addPlayer function to remove active players when they die or leave
- local function removeCharacter()
- if (player.Playing.Value == true) then
- alivePlayers = alivePlayers - 1
- player.Playing.Value = false
- end
- end
- player.CharacterRemoving:Connect(removeCharacter)
- end
- game:GetService('Players').PlayerAdded:Connect(addPlayer)
- -- Function that runs the lobby code
- function Lobby()
- Announce('Waiting for more players...')
- wait(5)
- -- Check if the num of players alive is more or equal to 2
- if table.getn(game.Players:GetPlayers()) >= minPlayers then
- -- Countdown for 5s and then start game
- for i =5, 0, -1 do
- Announce('Round starts in '.. i)
- wait(1)
- end
- startGame()
- else
- Announce('Waiting for more players...')
- end
- end
- -- Function that runs the start game code
- function startGame()
- -- Spawn a weapon for every spawn location we have
- for i, spawner in ipairs(workspace.WeaponSpawners:GetChildren()) do
- spawner.Spawn:Fire()
- end
- -- Spawn a vehicle for every spawn location we have
- for i, spawner in ipairs(workspace.VehicleSpawners:GetChildren()) do
- spawner.Spawn:Fire()
- end
- -- Count active players in game and set playing value to true
- for i, player in ipairs(game.Players:GetPlayers()) do
- alivePlayers = alivePlayers + 1
- player.Playing.Value = true
- -- Get teleport location and then teleport the player
- local teleLoc = CFrame.new(math.random(negCorner.X + 10, posCorner.X - 10),ground.Y + 100,math.random(negCorner.Z+10, posCorner.Z-10))
- player.Character.HumanoidRootPart.CFrame = teleLoc
- -- Clone the sword and give it to the player
- local swordClone = game.ServerStorage:FindFirstChild('Laser Sword'):Clone()
- swordClone.Parent = player:WaitForChild('Backpack')
- end
- state = statePLAYING
- end
- -- Function that runs the playing code
- function Playing()
- -- Announce the num of players still alive
- Announce(alivePlayers ..' still alive!')
- -- When there is one player left go to round-end state
- if (alivePlayers <= 1) then
- state = stateROUNDEND
- end
- end
- -- Function that runs the round end code
- function roundEnd()
- -- Check which player won
- for i, player in ipairs(game.Players:GetPlayers()) do
- if (player.Playing.Value == true) then
- victor = player
- break
- end
- end
- -- Celebrate winner or tie
- if victor then
- victor.Character.HumanoidRootPart.CFrame = workspace.SpawnBase.VictoryPedestal.CFrame + Vector3.new(0,3,0)
- Announce(victor.Name..'wins!')
- wait(10)
- if victor then
- victor:LoadCharacter()
- end
- else
- Announce('Tie?!')
- end
- -- Wait a bit and then reset victor
- wait(1)
- victor = nil
- -- Change state to lobby
- state = stateLOBBY
- -- For loop that destroys weapon at the end of every round
- for i, weapon in ipairs(workspace.Weapons:GetChildren()) do
- weapon:Destroy()
- end
- -- For loop that destroys vehicle at the end of every round
- for i, vehicle in ipairs(workspace.Vehicles:GetChildren()) do
- vehicle:Destroy()
- end
- end
- -- Create initial wait fo that the game has time to load
- wait(10)
- -- Infinite game while loop that changes depending on state value
- while(true) do
- wait(1)
- if (state == stateLOBBY) then
- Lobby()
- end
- if (state == statePLAYING) then
- Playing()
- end
- if (state == stateROUNDEND) then
- roundEnd()
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement