local function m(m)local S=script.Squad:Clone()S.Name=m and m or HttpService:GenerateGUID(false)S.Queue.Value="\083\111\108\111"S.Fill.Value=false S.Parent=SQUADS return S end local function S(m)for S,P in pairs(SQUADS:GetChildren())do if P.Players:FindFirstChild(m.Name)then return P end end end local function P(m)for S,P in pairs(SQUADS:GetChildren())do local j=P.Players:FindFirstChild(m.Name)if j then j:Destroy()if#P.Players:GetChildren()==0 then P:Destroy()else if not P.Players:FindFirstChild(P.Leader.Value)then P.Leader.Value=(P.Players:GetChildren())[1].Name end end break end end end local function j(m,j)local U=S(m)if U then P(m)end local l=Instance.new("\066\111\111\108\086\097\108\117\101")l.Name=m.Name l.Value=false l.Parent=j.Players if#j.Players:GetChildren()==1 then j.Leader.Value=m.Name elseif#j.Players:GetChildren()>1 then j.Queue.Value="\083\113\117\097\100"end end local function U(S)local P=m()j(S,P)REMOTES.MatchInfo:FireClient(S,"\076\101\097\118\101")end
-- 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
