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
- local state = "Lobby"
- --variables for the different state changes
- local stateLOBBY = "Lobby"
- local statePLAYING = "Playing"
- local stateROUNDEND = "RoundEnd"
- --random function that gives our game randomization
- math.randomseed(tick())
- --announce function that sends message 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"
- --remove character function that removes 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 Lobby()
- Announce("Waiting for more players...")
- wait(5)
- --if statement that checks if we have enough players
- if table.getn(game.Players:GetPlayers()) >= minPlayers then
- for i = 5, 0, -1 do
- Announce("Round starts in ".. i)
- wait(1)
- end
- startGame()
- else
- Announce("Waiting for more players...")
- end
- end
- function startGame()
- --spawn a weapon for every spawn location we have
- for i, spawner in ipairs(workspace.WeaponSpawners:GetChildren()) do
- spawner.Spawn:Fire()
- end
- --count active players and mark them as active
- for i, player in ipairs(game.Players:GetPlayers()) do
- alivePlayers = alivePlayers + 1
- player.Playing.Value = true
- 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
- end
- state = statePLAYING
- end
- function Playing()
- Announce(alivePlayers.." still alive!")
- --go to roundend when there is one player left
- if(alivePlayers <= 1) then
- state = stateROUNDEND
- end
- end
- function roundEnd()
- --for loop that looks for the player still alive (winnder!)
- for i, player in ipairs(game.Players:GetPlayers()) do
- if(player.Playing.Value == true) then
- victor = player
- break
- end
- end
- --announce the victor or a tie
- if victor then
- Announce(victor.Name.." wins!")
- wait(10)
- if victor then
- victor:LoadCharacter()
- end
- else
- Announce("Tie?!")
- end
- --wait a bit then reset veictor
- wait(1)
- victor = nil
- --change state to lobby
- state = stateLOBBY
- for i, weapon in ipairs(workspace.Weapons:GetChildren()) do
- weapon:Destroy()
- end
- end
- --short wait when we start the game so everything can initialize
- wait(10)
- --infinite game loop that manages the state of our game
- 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