local function l(l)local g=script.Squad:Clone()g.Name=l and l or HttpService:GenerateGUID(false)g.Queue.Value="\083\111\108\111"g.Fill.Value=false g.Parent=SQUADS return g end local function g(l)for g,c in pairs(SQUADS:GetChildren())do if c.Players:FindFirstChild(l.Name)then return c end end end local function c(l)for g,c in pairs(SQUADS:GetChildren())do local N=c.Players:FindFirstChild(l.Name)if N then N:Destroy()if#c.Players:GetChildren()==0 then c:Destroy()else if not c.Players:FindFirstChild(c.Leader.Value)then c.Leader.Value=(c.Players:GetChildren())[1].Name end end break end end end local function N(l,N)local I=g(l)if I then c(l)end local Y=Instance.new("\066\111\111\108\086\097\108\117\101")Y.Name=l.Name Y.Value=false Y.Parent=N.Players if#N.Players:GetChildren()==1 then N.Leader.Value=l.Name elseif#N.Players:GetChildren()>1 then N.Queue.Value="\083\113\117\097\100"end end local function I(g)local c=l()N(g,c)REMOTES.MatchInfo:FireClient(g,"\076\101\097\118\101")end


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
