This is a basic round script i made for a small project im working on, it’s not the final product of what i want it to be as i am still learning about how to make round systems.
It works fine as intended but im wondering if there are any ways to improve this code on either the client script or server script.
Client Script
local JoinRound = game:GetService("ReplicatedStorage").JoinRound
local RoundRemote = game:GetService("ReplicatedStorage")["RoundStarted/Ended"]
local Container = script.Parent.Container
local ButtonContainer = Container.ButtonContainer
local YesButton = ButtonContainer.YesButton
local NoButton = ButtonContainer.NoButton
local Announce = Container.Announce
local number
function onEventFire(Num,Text)
print("Event recieved")
local function creatimer()
for i = Num,0,-1 do
Announce.Text = string.format(Text,i)
number = i
print(number)
if i <= 0 then
Announce.Text = ""
end
task.wait(1)
end
end
taskcoro = coroutine.create(creatimer)
coroutine.resume(taskcoro)
Container.Visible = true
ButtonContainer.Visible = true
YesButton.MouseButton1Click:Connect(function()
JoinRound:FireServer()
Text = "The round will start in %s seconds"
ButtonContainer.Visible = false
end)
NoButton.MouseButton1Click:Connect(function()
Container.Visible = false
ButtonContainer.Visible = false
end)
end
function RoundStatus(RoundSeconds,Status)
if Status == "Started" and RoundSeconds ~= nil then
print("Started")
coroutine.close(taskcoro)
for i = RoundSeconds,0,-1 do
Announce.Text = string.format("The round will end in %s seconds",i)
if i <= 0 then
Announce.Text = ""
end
task.wait(1)
end
end
if Status == "Ended" and RoundSeconds == nil then
print("Ended")
Announce.Text = "Round ended!"
Container.Visible = true
task.wait(2)
Container.Visible = false
end
end
JoinRound.OnClientEvent:Connect(onEventFire)
RoundRemote.OnClientEvent:Connect(RoundStatus)
Server Script
local Players = game:GetService("Players")
local Maps = game:GetService("ServerStorage").Maps:GetChildren()
local JoinRound = game:GetService("ReplicatedStorage").JoinRound
local RoundRemote = game:GetService("ReplicatedStorage")["RoundStarted/Ended"]
local Debounce = false
local playerlist = {}
local playersalreadyfired = {}
local RoundDelay = 5
local IntermissionTime = 10
local SecondsPerRound = 10
local CurrentIntermissionTime
local RoundMap
JoinRound.OnServerEvent:Connect(function(player)
table.insert(playerlist,player)
player.Character:PivotTo(RoundMap.Spawn.CFrame * CFrame.new(0,3,0))
end)
while task.wait(RoundDelay) do
JoinRound:FireAllClients(IntermissionTime,"A round is starting in %s seconds, would you like to join?")
RoundMap = Maps[math.random(#Maps)]:Clone()
RoundMap.Parent = workspace
task.wait(IntermissionTime)
for i, player in pairs(playerlist) do
RoundRemote:FireClient(player,SecondsPerRound,"Started")
end
task.wait(SecondsPerRound)
for i, player in pairs(playerlist) do
if not table.find(playersalreadyfired,player) then
table.insert(playersalreadyfired,player)
RoundRemote:FireClient(player,nil,"Ended")
task.wait(.1)
table.remove(playersalreadyfired,1)
end
end
RoundMap:Destroy()
table.clear(playerlist)
end