local E for l,i in pairs(BonusRoundFrame.BuyMode:GetChildren())do i.Buy.MouseButton1Click:connect(function()game.ReplicatedStorage.BuyMode:FireServer(i.Name,E)end)end game.ReplicatedStorage.BuyMode.OnClientEvent:connect(function(E,l)local i=BonusRoundFrame.Title.Container.ModeContainer["\077\111\100\101"..E]i.Icon.Image=Icons[l]or""i.ModeName.Text=""i.QMark.Visible=false local h=BonusRoundFrame.Title.Container.OverheadContainer["\077\111\100\101"..E]h.Buy.Visible=false h.Purchased.Visible=true end)local function l(l,i,h,q)BonusRoundFrame.Title.Container.ModeContainer:ClearAllChildren()BonusRoundFrame.Title.Container.OverheadContainer:ClearAllChildren()for l,i in pairs(l)do local h=script.GameMode:Clone()h.QMark.Visible=i=="\076\111\099\107\101\100"or i=="\082\097\110\100\111\109"h.ModeName.Text=(i=="\076\111\099\107\101\100"or i=="\082\097\110\100\111\109")and"\082\097\110\100\111\109"or""h.Locked.Visible=i=="\076\111\099\107\101\100"h.Icon.Image=Icons[i]or""if l==1 then h.BackgroundColor3=Color3.new(.15686274509804,.15686274509804,.15686274509804)h.BorderColor3=Color3.new(.098039215686275,.098039215686275,.098039215686275)h.ModeName.TextColor3=Color3.new(.23137254901961,.23137254901961,.23137254901961)h.QMark.TextColor3=Color3.new(.23137254901961,.23137254901961,.23137254901961)h.Icon.Image=BWIcons[i]or""end h.Position=UDim2.new(0,(l-1)*83,0,4)h.Name="\077\111\100\101"..l h.Parent=BonusRoundFrame.Title.Container.ModeContainer local q=script.Overhead:Clone()local z=l==2 local y=i=="\082\097\110\100\111\109"local Q=i~="\082\097\110\100\111\109"and i~="\076\111\099\107\101\100"local Z=y and not z q.Name="\077\111\100\101"..l q.Buy.Visible=Z if Z then q.Buy.MouseButton1Click:connect(function()if E~=l then BonusRoundFrame.BuyMode.Visible=true BonusRoundFrame.Loading.Visible=false BonusRoundFrame.Voting.Visible=false BonusRoundFrame.Nothing.Visible=false E=l for E,l in pairs(BonusRoundFrame.Title.Container.OverheadContainer:GetChildren())do l.Buy.Style=l==q and Enum.ButtonStyle.RobloxRoundButton or Enum.ButtonStyle.RobloxRoundDefaultButton end else E=nil q.Buy.Style=Enum.ButtonStyle.RobloxRoundDefaultButton BonusRoundFrame.BuyMode.Visible=false BonusRoundFrame.Loading.Visible=Stage=="\076\111\097\100\105\110\103"BonusRoundFrame.Voting.Visible=Stage=="\086\111\116\105\110\103"BonusRoundFrame.Nothing.Visible=Stage=="\078\111\116\104\105\110\103"end end)end q.Locked.Visible=i=="\076\111\099\107\101\100"and not z q.Next.Visible=z q.Purchased.Visible=Q and not z q.Position=UDim2.new(0,(l-1)*83,0,-2)q.Parent=BonusRoundFrame.Title.Container.OverheadContainer end if q then BonusRoundFrame.Nothing.Visible=false if h then RandomModes=h BonusRoundFrame.Voting.Visible=true Stage="\086\111\116\105\110\103"else RandomModes={}BonusRoundFrame.Voting.Visible=false BonusRoundFrame.Loading.Visible=true Stage="\076\111\097\100\105\110\103"BonusRoundFrame.Loading[i].Visible=true end UpdateVotes()end end game.ReplicatedStorage.VoteBonusRound.OnClientEvent:connect(function(E,i,h)l(E,i,h,true)BonusRoundFrame.Visible=true end)local i=game.ReplicatedStorage.GetQueue:InvokeServer()l(i)


local BuyingIndex;
for _,Frame in pairs(BonusRoundFrame.BuyMode:GetChildren()) do
	Frame.Buy.MouseButton1Click:connect(function()
		game.ReplicatedStorage.BuyMode:FireServer(Frame.Name,BuyingIndex);
	end)
end

game.ReplicatedStorage.BuyMode.OnClientEvent:connect(function(Index,GameMode)
	local ModeFrame = BonusRoundFrame.Title.Container.ModeContainer["Mode"..Index];
	ModeFrame.Icon.Image = Icons[GameMode] or "";
	ModeFrame.ModeName.Text = "";
	ModeFrame.QMark.Visible = false;
	local Overhead = BonusRoundFrame.Title.Container.OverheadContainer["Mode"..Index];
	Overhead.Buy.Visible = false;
	Overhead.Purchased.Visible = true;
end)


local function UpdateQueue(BonusRoundQueue,CurrentMode,RandomM,StartVoting)
	BonusRoundFrame.Title.Container.ModeContainer:ClearAllChildren();
	BonusRoundFrame.Title.Container.OverheadContainer:ClearAllChildren();
	for Index,GameModeName in pairs(BonusRoundQueue) do
		local ModeFrame = script.GameMode:Clone();
		ModeFrame.QMark.Visible = (GameModeName=="Locked"or GameModeName=="Random");
		ModeFrame.ModeName.Text = (GameModeName=="Locked"or GameModeName=="Random") and "Random" or "";
		ModeFrame.Locked.Visible = (GameModeName=="Locked");
		ModeFrame.Icon.Image = Icons[GameModeName] or "";
		if Index==1 then
			ModeFrame.BackgroundColor3 = Color3.new(40/255,40/255,40/255);
			ModeFrame.BorderColor3 = Color3.new(25/255,25/255,25/255);
			ModeFrame.ModeName.TextColor3 = Color3.new(59/255,59/255,59/255);
			ModeFrame.QMark.TextColor3 = Color3.new(59/255,59/255,59/255);
			ModeFrame.Icon.Image = BWIcons[GameModeName]or"";
		end
		ModeFrame.Position = UDim2.new(0,(Index-1)*(76+7),0,4);
		ModeFrame.Name = "Mode"..Index;
		ModeFrame.Parent = BonusRoundFrame.Title.Container.ModeContainer;
		
		local OverheadFrame = script.Overhead:Clone();
		local NextUp = (Index==2);		
		local Buy = (GameModeName=="Random");
		local Purchased = (GameModeName~="Random"and GameModeName~="Locked");
		local Buyable = (Buy and not NextUp);
		OverheadFrame.Name = "Mode"..Index;
		
		OverheadFrame.Buy.Visible = Buyable;
		if Buyable then
			OverheadFrame.Buy.MouseButton1Click:connect(function()
				if BuyingIndex ~= Index then
					BonusRoundFrame.BuyMode.Visible = true;
					BonusRoundFrame.Loading.Visible = false;
					BonusRoundFrame.Voting.Visible = false;
					BonusRoundFrame.Nothing.Visible = false;
					BuyingIndex = Index;

					for _,OV in pairs(BonusRoundFrame.Title.Container.OverheadContainer:GetChildren()) do
						OV.Buy.Style = (OV==OverheadFrame and Enum.ButtonStyle.RobloxRoundButton) or Enum.ButtonStyle.RobloxRoundDefaultButton;
					end
					
				else
					BuyingIndex = nil;
					OverheadFrame.Buy.Style = Enum.ButtonStyle.RobloxRoundDefaultButton;
					BonusRoundFrame.BuyMode.Visible = false;
					BonusRoundFrame.Loading.Visible = Stage=="Loading";
					BonusRoundFrame.Voting.Visible = Stage=="Voting";
					BonusRoundFrame.Nothing.Visible = Stage=="Nothing";
				end;
				-- game.ReplicatedStorage.BuyMode:FireServer(Index);
			end)
		end

		OverheadFrame.Locked.Visible = (GameModeName=="Locked" and not NextUp);
		OverheadFrame.Next.Visible = NextUp;
		OverheadFrame.Purchased.Visible = Purchased and not NextUp;
		OverheadFrame.Position = UDim2.new(0,(Index-1)*(76+7),0,-2);
		OverheadFrame.Parent = BonusRoundFrame.Title.Container.OverheadContainer;
			
	end
	
	if StartVoting then
		BonusRoundFrame.Nothing.Visible = false;
		if RandomM then
			RandomModes = RandomM;
			BonusRoundFrame.Voting.Visible = true;
			Stage = "Voting";
		else
			RandomModes = {};
			BonusRoundFrame.Voting.Visible = false;
			BonusRoundFrame.Loading.Visible = true;
			Stage = "Loading";
			BonusRoundFrame.Loading[CurrentMode].Visible = true;
		end;
		UpdateVotes();
	end;
	
end

game.ReplicatedStorage.VoteBonusRound.OnClientEvent:connect(function(BonusRoundQueue,CurrentMode,RandomM)
	UpdateQueue(BonusRoundQueue,CurrentMode,RandomM,true);
	BonusRoundFrame.Visible = true;
end)

local BonusRoundQueue = game.ReplicatedStorage.GetQueue:InvokeServer();
UpdateQueue(BonusRoundQueue);

