SERVER STORAGE | MODULES
-- Services
local REP = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
-- Root
local module = {}
-- Data
module.InRound = {}
module.MINIMUM_PLAYER_COUNT = 2
module.INTERMISSION_TIME = 10
-- Instances
local REP_Values = REP.GameAssets.Values
local StatusValue = REP_Values.Status
local Map = workspace.Map
local Lobby = workspace.Lobby
-- Functions
function module:Init()
Players.PlayerRemoving:Connect(function(Player)
self:_RemoveFromRound(Player.UserId)
end)
self:_StartRound()
end
function module:_StartRound()
self:_WaitForPlayers()
self:_Intermission()
if #Players:GetPlayers() < self.MINIMUM_PLAYER_COUNT then self:_StartRound()
return end
self:_AddToRound()
self:_Begin()
end
function module:_WaitForPlayers()
if #Players:GetPlayers() >= self.MINIMUM_PLAYER_COUNT then return end
repeat
for NumberOfDots = 1, 3 do
StatusValue.Value = "Waiting for Players" .. string.rep(".",
NumberOfDots)
task.wait(0.25)
end
until #Players:GetPlayers() >= self.MINIMUM_PLAYER_COUNT
end
function module:_Intermission()
for i = self.INTERMISSION_TIME, 1, -1 do
StatusValue.Value = "Intermission: " .. i
task.wait(1)
end
end
function module:_AddToRound()
for _, Player: Player in pairs(Players:GetPlayers()) do
--if Player:GetAttribute("AFK") then continue end
Player.Character.Humanoid.Died:Connect(function()
self:_RemoveFromRound(Player.UserId)
end)
table.insert(self.InRound, Player.UserId)
end
end
function module:_TeleportPlayers(Location: string)
local AvailableSpawns = {}
local Spawns = {}
if Location == "Map" then
Spawns = Map.SpawnPoints:GetChildren()
elseif Location == "Lobby" then
Spawns = Lobby.SpawnPoints:GetChildren()
end
for _, BasePart: BasePart in pairs(Spawns) do
table.insert(AvailableSpawns, BasePart)
end
for _, PlayerId: number in pairs(self.InRound) do
local Player = Players:GetPlayerByUserId(PlayerId)
local RandomIndex = math.random(1, #AvailableSpawns)
local RandomSpawn = AvailableSpawns[RandomIndex]
table.remove(AvailableSpawns, RandomIndex)
Player.Character:PivotTo(RandomSpawn:GetPivot() + Vector3.new(0,2,0))
end
end
function module:_Begin()
StatusValue.Value = "Starting..."
self:_SetHumanoidStats(0,0)
self:_TeleportPlayers("Map")
task.wait(1)
for i = 3, 1, -1 do
StatusValue.Value = "Starting... " .. i
task.wait(1)
end
StatusValue.Value = "GO!"
self:_SetHumanoidStats(16, 7.2)
repeat
task.wait(1)
local ShouldEnd = self:_CheckForWinner()
if ShouldEnd then break end
until true == false
self:_TeleportPlayers("Lobby")
task.wait(2)
self:_StartRound()
end
function module:_CheckForWinner()
if #self.InRound == 1 then
local Winner = Players:GetPlayerByUserId(self.InRound[1])
StatusValue.Value = Winner.Name .. " wins!"
return true
elseif #self.InRound == 0 then
StatusValue.Value = "It was a tie!"
return true
elseif #self.InRound > 1 then
StatusValue.Value = `{#self.InRound} Players Left`
return false
end
end
function module:_RemoveFromRound(PlayerId: number)
local Index = nil
for i, P_Id: number in pairs(self.InRound) do
if P_Id == PlayerId then Index = i break end
end
if not Index then return end
table.remove(self.InRound, Index)
end
function module:_SetHumanoidStats(WalkSpeed: number, JumpHeight: number)
for _, PlayerId: number in pairs(self.InRound) do
local Player = Players:GetPlayerByUserId(PlayerId)
Player.Character.Humanoid.WalkSpeed = WalkSpeed
Player.Character.Humanoid.JumpHeight = JumpHeight
end
end
-- Return Root
return module
-- Services
local SS = game:GetService("ServerStorage")
-- Modules
local SS_Modules = SS.Modules
local RoundSystem = require(SS_Modules.Gameplay.RoundSystem)
-- Code
RoundSystem:Init()