zkPkwDAF

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 4

-- Round System -SkyeDaCat- On Youtube

local config = require(script:WaitForChild("Config"))

local players = game:GetService("Players")


local replicatedStorage = game:GetService("ReplicatedStorage")

local updateTxtEvent = replicatedStorage.RoundSystemEvents.UpdateText

local queuedPlayers = {}
local activePlayers = {}

local gameActive = script:WaitForChild("GameActive")


local countingDown = script:WaitForChild("CountingDown")

local spawns = game.Workspace:WaitForChild("Spawns")

local function updateText(msg, visible)


updateTxtEvent:FireAllClients(msg, visible)
end

local function queuePlayers()


if gameActive.Value == false then
for i, plr in pairs(players:GetPlayers()) do
if not table.find(queuedPlayers, plr.UserId) then
table.insert(queuedPlayers, plr.UserId)
print(plr.Name.." Has been added to the queue!")
end
end
end
end

players.PlayerAdded:Connect(queuePlayers)

local function removePlayer(plr)


local id = plr.UserId

table.remove(queuedPlayers, table.find(queuedPlayers, id))


table.remove(activePlayers, table.find(activePlayers, id))
print(plr.Name.." Have been removed from the game.")
end

local function CheckForPlayers()


while task.wait(1) do
if gameActive.Value == true or countingDown.Value == true then return
end

queuePlayers()

if #queuedPlayers >= config.MinimumPlayers then


countingDown.Value = true
print("Countdown started!")
else
updateText("Waiting for players...", true)
end
end
end

local function gameStart()


local playersSetup = {}

while true do
for i, plr in pairs(players:GetPlayers()) do
local userId = plr.UserId

if table.find(queuedPlayers, userId) and not playersSetup[userId]


then
table.remove(queuedPlayers, table.find(queuedPlayers,
userId))
table.insert(activePlayers, userId)
playersSetup[userId] = true

local char = plr.Character


local humanoid = char.Humanoid
local HRP = char.HumanoidRootPart
local spawnNum = math.random(1, #spawns:GetChildren())
local spawnPart = spawns[spawnNum]

HRP.CFrame = spawnPart.CFrame + Vector3.new(0,5,0)

local diedConnection
diedConnection = humanoid.Died:Connect(function()
removePlayer(plr)
diedConnection:Disconnect()
end)
end
end

if #activePlayers == 1 then
local winner = players:GetPlayerByUserId(activePlayers[1])
print(winner.Name, "has won!")

updateText(winner.Name.."has won the match!", true)

task.wait(5)

winner.Character.HumanoidRootPart.CFrame =
workspace.SpawnLocation.CFrame + Vector3.new(0,5,0)
winner.leaderstats.Wins.Value += 1

gameActive.Value = false
CheckForPlayers()
break
end

task.wait(1)
end
end

countingDown.Changed:Connect(function()
if countingDown.Value == true then
for i=config.countdownTime,0,-1 do

if #queuedPlayers < config.MinimumPlayers then


countingDown.Value = false
CheckForPlayers()
return
end
task.wait(1)
updateText("Starting in "..i.." seconds!", true)
end

if #queuedPlayers < config.MinimumPlayers then


countingDown.Value = false
task.wait(1)
CheckForPlayers()
return
end

countingDown.Value = false
gameActive.Value = false
updateText("", false)
print("Countdown ended")
gameStart()
end
end)

CheckForPlayers()

players.PlayerRemoving:Connect(removePlayer)

-- Config

local config = {
MinimumPlayers = 1;
countdownTime = 15;
}

return config

-- leaderstats

game.Players.PlayerAdded:Connect(function(plr)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"

local wins = Instance.new("IntValue", leaderstats)


wins.Name = "Wins"
wins.Value = 0

local kills = Instance.new("IntValue", leaderstats)


kills.Name = "Kills"
kills.Value = 0

leaderstats.Parent = plr
end)

-- local script

local replicatedStorage = game:GetService("ReplicatedStorage")


local updateTxtEvent = replicatedStorage.RoundSystemEvents.UpdateText
local textLabel = script.Parent
updateTxtEvent.OnClientEvent:Connect(function(msg, visible)
textLabel.Text = msg
textLabel.Visible = visible
end)

You might also like