so basically I have this round system I made. But it has some bugs.
And i was wondering how I can improve this code. (sorry for the long script)
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TFM = require(ReplicatedStorage:WaitForChild("Modules"):WaitForChild("TFM"))
local voting = require(script.Voting)
local RequiredPlayers = 2
local function RequiredPlayersFunc()
return #Players:GetPlayers() >= RequiredPlayers
end
local function ChooseRandomNumber()
return math.random(1, 10)
end
while true do
repeat
task.wait(1)
ReplicatedStorage:WaitForChild("Values"):WaitForChild("Status").Value = "You need atleast "..RequiredPlayers.." or more players to start!"
until RequiredPlayersFunc()
local chosenMap = voting.MapVoting(10)
local clonedMap = chosenMap:Clone()
clonedMap.Name = "Map"
clonedMap.Parent = game.Workspace.Game.Map
for _, plr in pairs(Players:GetPlayers()) do
if not plr:FindFirstChild("AFK") then
local stringValue = Instance.new("StringValue")
stringValue.Parent = ReplicatedStorage:WaitForChild("InMatch")
stringValue.Name = plr.Name
stringValue.Value = plr.Name
end
end
for _, v in pairs(ReplicatedStorage:WaitForChild("InMatch"):GetChildren()) do
if v:IsA("StringValue") then
if Players:FindFirstChild(v.Name) then
Players:FindFirstChild(v.Name).Character:MoveTo(clonedMap:FindFirstChild("MapTP").Position)
end
end
end
-- cutscene
task.wait(1)
ReplicatedStorage:WaitForChild("Events"):WaitForChild("CutsceneEvent"):FireAllClients(clonedMap)
ReplicatedStorage:WaitForChild("Values"):WaitForChild("Status").Value = "GAME STARTS"
for i = 20, 0, -1 do
ReplicatedStorage:WaitForChild("Values"):WaitForChild("Timer").Value = i
task.wait(1)
end
ReplicatedStorage:WaitForChild("Values"):WaitForChild("GameStart").Value = true
clonedMap:FindFirstChild("MapBarrier"):Destroy()
ReplicatedStorage:WaitForChild("Values"):WaitForChild("Status").Value = "GAME ENDS"
coroutine.wrap(function()
for i = clonedMap:GetAttribute("TimeEnd"), 0, -1 do
if ReplicatedStorage:WaitForChild("Values"):WaitForChild("GameStart").Value == true then
local ConvertT = TFM:Convert(i, "Default", true)
ReplicatedStorage:WaitForChild("Values"):WaitForChild("Timer").Value = ConvertT
task.wait(1)
end
end
print("game ended")
ReplicatedStorage:WaitForChild("Values"):WaitForChild("GameStart").Value = false
clonedMap:Destroy()
for _, v in pairs(ReplicatedStorage:WaitForChild("InMatch"):GetChildren()) do
if v:IsA("StringValue") then
v:Destroy()
end
end
end)()
for _, player in pairs(Players:GetPlayers()) do
if not player:FindFirstChild("AFK")
and ReplicatedStorage:WaitForChild("Values"):WaitForChild("GameStart").Value == true and #ReplicatedStorage.InMatch:GetChildren() >= 2 then
local playerGui = player.PlayerGui
local character = player.Character
-- Start Main Game Function
while true do
if ReplicatedStorage:WaitForChild("Values"):WaitForChild("GameStart").Value == false then break end
local numberA = math.random(0, 10)
local numberB = math.random(0, 10)
local answer = numberA + numberB
ReplicatedStorage.Events.Question:FireAllClients(numberA, numberB)
for _, block in clonedMap:WaitForChild("Plates"):GetChildren() do
if block:IsA("BasePart") then
block.BrickColor = BrickColor.random()
block.Transparency = 0
block.MainUI.Enabled = true
block.CanCollide = true
block.MainUI.Answer.Text = math.random(1, 10+10)
end
end
for i = 10, 0, -1 do
local ConvertT = TFM:Convert(i, "Default", true)
ReplicatedStorage:WaitForChild("Values"):WaitForChild("QuestionTimer").Value = ConvertT
task.wait(1)
end
print(answer)
for _, block in clonedMap:WaitForChild("Plates"):GetChildren() do
if block:IsA("BasePart") then
local isCorrectBlock = (tonumber(block.MainUI.Answer.Text) == answer)
block.CanCollide = isCorrectBlock
block.Transparency = isCorrectBlock and 0 or 1
block.MainUI.Enabled = if isCorrectBlock then true else false
end
end
task.wait(3)
end
end
end
end