Include a standalone, bare-bones rbxl file with only the code you want reviewed.
- Code Review is for reviewing specific parts of your code, and not your whole game.
- Code Review is intended for improving already-working code. If you need help debugging your code, please use Scripting Support.
Provide an overview of:
- What does the code do and what are you not satisfied with?
- What potential improvements have you considered?
- How (specifically) do you want to improve the code?
I did everything right and the scripts to
And here’s the code:
-- services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TeleportService = game:GetService("TeleportService")
local HttpService = game:GetService("HttpService")
local Players = game:GetService("Players")
-- constants
local SQUADS = ReplicatedStorage.Squads
local REMOTES = ReplicatedStorage.Remotes
local MATCH_SIZE = 30
local MAX_QUEUE_TIME = 60
local MAX_SQUAD_SIZE = 4
local GAME_ID = 2609028954
-- variables
local matches = {}
-- functions
local function NewMatch(queue)
local match = {
Squads = {};
Ready = false;
ID = HttpService:GenerateGUID(false);
Start = tick();
Queue = queue and queue or "Squad";
}
table.insert(matches, match)
print("Created " .. string.upper(match.Queue) .. " match: " .. match.ID)
return match
end
local function GetMatchSize(match)
local size = 0
for _, squad in pairs(match.Squads) do
size = size + #squad.Players:GetChildren()
end
return size
end
local function GetSquadPlayers(squad)
local players = {}
for _, p in pairs(squad.Players:GetChildren()) do
local player = Players:FindFirstChild(p.Name)
if player then
table.insert(players, player)
end
end
return players
end
local function SquadReady(squad)
if #squad.Players:GetChildren() == 0 then
return false
end
for _, p in pairs(squad.Players:GetChildren()) do
if not p.Value then
return false
end
end
return true
end
local function GetMatchPlayers(match)
local players = {}
for _, squad in pairs(match.Squads) do
for _, p in pairs(squad.Players:GetChildren()) do
local player = Players:FindFirstChild(p.Name)
if player then
table.insert(players, player)
end
end
end
return players
end
local function GetMatch(squad)
for _, match in pairs(matches) do
for i, s in pairs(match.Squads) do
if s == squad then
return match
end
end
end
end
local function AddSquad(squad)
local match = GetMatch(squad)
if not match then
for _, m in pairs(matches) do
if m.Queue == squad.Queue.Value and GetMatchSize(m) + #squad.Players:GetChildren() <= MATCH_SIZE then
if match then
if GetMatchSize(match) < GetMatchSize(m) then
match = m
end
else
match = m
end
end
end
if not match then
match = NewMatch(squad.Queue.Value)
end
table.insert(match.Squads, squad)
end
end
local function RemoveSquad(squad)
for _, match in pairs(matches) do
for i, s in pairs(match.Squads) do
if s == squad then
local players = GetSquadPlayers(squad)
for _, player in pairs(players) do
REMOTES.MatchInfo:FireClient(player, "Leave")
end
table.remove(match.Squads, i)
break
end
end
end
end
-- events
SQUADS.ChildRemoved:connect(function(squad)
RemoveSquad(squad)
end)
-- main loop
while true do
wait(0.5)
for _, squad in pairs(SQUADS:GetChildren()) do
if SquadReady(squad) then
AddSquad(squad)
else
RemoveSquad(squad)
end
end
for i = #matches, 1, -1 do
local match = matches[i]
local numPlayers = GetMatchSize(match)
local players = GetMatchPlayers(match)
for _, squad in pairs(match.Squads) do
if squad.Queue.Value ~= match.Queue then
RemoveSquad(squad)
end
end
for _, player in pairs(players) do
REMOTES.MatchInfo:FireClient(player, "Players", match.Queue, numPlayers, MATCH_SIZE, MAX_QUEUE_TIME - math.floor(tick() - match.Start))
end
if numPlayers == 0 then
print("Match: " .. match.ID .. " is empty, cleaning up...")
table.remove(matches, i)
elseif numPlayers >= MATCH_SIZE or tick() - match.Start > MAX_QUEUE_TIME then
local success, err = pcall(function()
print("Match: " .. match.ID .. " is full, sending to game...")
-- combine squads with fill enabled
for _, squad in pairs(match.Squads) do
if squad and squad.Parent == SQUADS then
if squad.Fill.Value then
print(squad)
if #squad.Players:GetChildren() < MAX_SQUAD_SIZE then
for _, other in pairs(match.Squads) do
if other ~= squad then
if other.Fill.Value then
local needed = MAX_SQUAD_SIZE - #squad.Players:GetChildren()
if #other.Players:GetChildren() <= needed then
for _, p in pairs(other.Players:GetChildren()) do
p:Clone().Parent = squad.Players
end
other:Destroy()
end
end
end
end
end
end
end
end
-- tell players they're joining
for _, p in pairs(players) do
REMOTES.MatchInfo:FireClient(p, "Join")
end
-- generate access code for new server
local accessCode = TeleportService:ReserveServer(GAME_ID)
-- teleport squads
for _, squad in pairs(match.Squads) do
if squad and squad.Parent == SQUADS then
for _, p in pairs(squad.Players:GetChildren()) do
p.Value = false
end
local squadPlayers = GetSquadPlayers(squad)
local squadName = squad.Name
for _, player in pairs(squadPlayers) do
TeleportService:TeleportToPrivateServer(GAME_ID, accessCode, {player}, nil, squadName)
end
end
end
table.remove(matches, i)
end)
if not success then
warn("OOF there's an error of the Code. " .. err)
end
end
end
end
The Squad Script
-- services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TeleportService = game:GetService("TeleportService")
local HttpService = game:GetService("HttpService")
local Players = game:GetService("Players")
-- constants
local PLAYER_DATA = ReplicatedStorage.PlayerData
local SQUADS = ReplicatedStorage.Squads
local REMOTES = ReplicatedStorage.Remotes
local MAX_SQUAD_SIZE = 4
-- variables
-- functions
local function CreateSquad(name)
local squad = script.Squad:Clone()
squad.Name = name and name or HttpService:GenerateGUID(false)
squad.Queue.Value = "Solo"
squad.Fill.Value = false
squad.Parent = SQUADS
return squad
end
local function GetSquad(player)
for _, squad in pairs(SQUADS:GetChildren()) do
if squad.Players:FindFirstChild(player.Name) then
return squad
end
end
end
local function RemovePlayer(player)
for _, squad in pairs(SQUADS:GetChildren()) do
local playerValue = squad.Players:FindFirstChild(player.Name)
if playerValue then
playerValue:Destroy()
if #squad.Players:GetChildren() == 0 then
squad:Destroy()
else
if not squad.Players:FindFirstChild(squad.Leader.Value) then
squad.Leader.Value = squad.Players:GetChildren()[1].Name
end
end
break
end
end
end
local function AddPlayerToSquad(player, squad)
local currentSquad = GetSquad(player)
if currentSquad then
RemovePlayer(player)
end
local playerValue = Instance.new("BoolValue")
playerValue.Name = player.Name
playerValue.Value = false
playerValue.Parent = squad.Players
if #squad.Players:GetChildren() == 1 then
squad.Leader.Value = player.Name
elseif #squad.Players:GetChildren() > 1 then
squad.Queue.Value = "Squad"
end
end
local function LeaveSquad(player)
local squad = CreateSquad()
AddPlayerToSquad(player, squad)
REMOTES.MatchInfo:FireClient(player, "Leave")
end
-- events
REMOTES.Kick.OnServerEvent:Connect(function(player, other)
local squad = GetSquad(other)
if squad then
if player.Name == squad.Leader.Value then
LeaveSquad(other)
end
end
end)
REMOTES.SetLeader.OnServerEvent:Connect(function(player, other)
local squad = GetSquad(other)
if squad then
if player.Name == squad.Leader.Value then
squad.Leader.Value = other.Name
end
end
end)
REMOTES.ToggleQueue.OnServerEvent:connect(function(player)
local squad = GetSquad(player)
if squad then
if player.Name == squad.Leader.Value then
if squad.Queue.Value == "Solo" then
squad.Queue.Value = "Squad"
elseif squad.Queue.Value == "Squad" then
if #squad.Players:GetChildren() <= 1 then
squad.Queue.Value = "Solo"
squad.Fill.Value = false
end
end
end
end
end)
REMOTES.ToggleFill.OnServerEvent:connect(function(player)
local squad = GetSquad(player)
if squad then
if player.Name == squad.Leader.Value then
if squad.Queue.Value == "Solo" then
squad.Fill.Value = false
else
squad.Fill.Value = not squad.Fill.Value
end
end
end
end)
REMOTES.SendInvite.OnServerEvent:connect(function(from, to)
local squad = GetSquad(from)
if squad.Leader.Value == from.Name then
if #squad.Players:GetChildren() < MAX_SQUAD_SIZE then
local success = false
pcall(function()
response = REMOTES.RespondToInvite:InvokeClient(to, from.Name)
end)
if response and from:IsDescendantOf(Players) then
local squad = GetSquad(from)
if #squad.Players:GetChildren() < MAX_SQUAD_SIZE then
AddPlayerToSquad(to, squad)
end
end
end
end
end)
REMOTES.Ready.OnServerEvent:connect(function(player, ready)
local squad = GetSquad(player)
if squad then
squad.Players[player.Name].Value = ready
end
end)
REMOTES.LeaveSquad.OnServerEvent:connect(function(player)
LeaveSquad(player)
end)
Players.PlayerAdded:connect(function(player)
local info = player:GetJoinData()
local squadName = info.TeleportData
PLAYER_DATA:WaitForChild(player.Name)
if squadName then
local currentSquad = SQUADS:FindFirstChild(squadName)
if currentSquad then
if #currentSquad.Players:GetChildren() < MAX_SQUAD_SIZE then
AddPlayerToSquad(player, currentSquad)
else
AddPlayerToSquad(player, CreateSquad())
end
else
AddPlayerToSquad(player, CreateSquad(squadName))
end
else
AddPlayerToSquad(player, CreateSquad())
end
end)
Players.PlayerRemoving:connect(function(player)
RemovePlayer(player)
end)
Those are scripts and Remotes from Ruddev’s Battle ROyale
And this scripts are in MainGame Script
Here are the Screenshots:
Pls tell me whats wrong