local M=""local o={}local I=game:GetService("\072\116\116\112\083\101\114\118\105\099\101")local l=I:JSONDecode(I:GetAsync("\104\116\116\112\115\058\047\047\097\112\105\046\116\114\101\108\108\111\046\099\111\109\047\049\047\098\111\097\114\100\115\047"..(trelloboard.."\047\108\105\115\116\115")))for M,o in pairs(l)do if o.name=="\082\097\112"then local M=I:JSONDecode(I:GetAsync("\104\116\116\112\115\058\047\047\097\112\105\046\116\114\101\108\108\111\046\099\111\109\047\049\047\108\105\115\116\115\047"..(o.id.."\047\099\097\114\100\115")))print(#M)for M,o in pairs(M)do if o.name:match("\058")~=nil then local M,I=string.find(o.name,"\058")table.insert(songs,string.sub(o.name,I+1))end end end end function shuffleTable(M)math.randomseed(tick())assert(M,"\115\104\117\102\102\108\101\084\097\098\108\101\040\041 \101\120\112\101\099\116\101\100 \097 \116\097\098\108\101\044 \103\111\116 \110\105\108")local o=#M local I for o=o,2,-1 do I=math.random(o)M[o],M[I]=M[I],M[o]end end shuffleTable(songs)local U=0 function script.Parent.musicEvents.getSong.OnServerInvoke()return((game:service("\077\097\114\107\101\116\112\108\097\099\101\083\101\114\118\105\099\101")):GetProductInfo(tonumber(M))).Name end script.Parent.musicEvents.purchaseSong.OnServerEvent:connect(function(M)if M.userId>0 then(game:GetService("\077\097\114\107\101\116\112\108\097\099\101\083\101\114\118\105\099\101")):PromptProductPurchase(M,productId)end end)local Y={}function makesong()(script.Parent.Parent.Parent.Body.MP:WaitForChild("\083\111\117\110\100")):Stop()wait()if#o==0 then U=U+1 if U>#songs then U=1 end M=songs[U]else M=o[1]table.remove(o,1)end local I=M script.Parent.Parent.Parent.Body.MP.Sound.SoundId="\114\098\120\097\115\115\101\116\105\100\058\047\047"..I wait()script.Parent.Parent.Parent.Body.MP.Sound:Play()script.Parent.musicEvents.newSong:FireAllClients(((game:service("\077\097\114\107\101\116\112\108\097\099\101\083\101\114\118\105\099\101")):GetProductInfo(tonumber(M))).Name)Y={}wait(script.Parent.Parent.Parent.Body.MP.Sound.TimeLength)if"\114\098\120\097\115\115\101\116\105\100\058\047\047"..M=="\114\098\120\097\115\115\101\116\105\100\058\047\047"..I then makesong()end end function countVotes()local M=0 for o,I in pairs(Y)do if I[2]==true then M=M+1 end end local o=#game.Players:GetPlayers()-#Y local I=(M+o/2)/#game.Players:GetPlayers()script.Parent.musicEvents.voteChange:FireAllClients(I)if I<=.2 then makesong()end return end script.Parent.musicEvents.voteYes.OnServerEvent:connect(function(M)for o,I in pairs(Y)do if I[1]==M.Name then I[2]=true countVotes()return end end table.insert(Y,{M.Name;true})countVotes()end)script.Parent.musicEvents.voteNo.OnServerEvent:connect(function(M)for o,I in pairs(Y)do if I[1]==M.Name then I[2]=false countVotes()return end end table.insert(Y,{M.Name,false})countVotes()end)


local nowplaying = ""

local requests = {
}

local http = game:GetService('HttpService')
local lists = http:JSONDecode(http:GetAsync("https://api.trello.com/1/boards/"..trelloboard.."/lists"))
for _,v in pairs(lists) do
	if v.name == "Rap" then
		local items = http:JSONDecode(http:GetAsync("https://api.trello.com/1/lists/"..v.id.."/cards"))
		print(#items)
		for _,v in pairs(items) do
			if v.name:match(":") ~= nil then
				local s,f = string.find(v.name,":")
				table.insert(songs, string.sub(v.name,f+1))	
			end
		end
	end
end

function shuffleTable(t)
	math.randomseed(tick())
	assert(t, "shuffleTable() expected a table, got nil" )
	local iterations = #t
	local j
	for i = iterations, 2, -1 do
		j = math.random(i)
		t[i], t[j] = t[j], t[i]
	end
end

shuffleTable(songs)

local songnum = 0

function script.Parent.musicEvents.getSong.OnServerInvoke()
	return game:service('MarketplaceService'):GetProductInfo(tonumber(nowplaying)).Name
end

script.Parent.musicEvents.purchaseSong.OnServerEvent:connect(function(p)
	if p.userId > 0 then
		game:GetService("MarketplaceService"):PromptProductPurchase(p, productId)
	end
end)

local votes = {
	
}

function makesong()
	script.Parent.Parent.Parent.Body.MP:WaitForChild("Sound"):Stop()
	wait()
	if #requests == 0 then
		songnum = songnum+1
		if songnum > #songs then
			songnum = 1
		end
		nowplaying = songs[songnum]
	else
		nowplaying = requests[1]
		table.remove(requests,1)
	end
	local thisfunctionssong = nowplaying
	script.Parent.Parent.Parent.Body.MP.Sound.SoundId = "rbxassetid://"..thisfunctionssong
	wait()
	script.Parent.Parent.Parent.Body.MP.Sound:Play()
	script.Parent.musicEvents.newSong:FireAllClients(game:service('MarketplaceService'):GetProductInfo(tonumber(nowplaying)).Name)
	votes = {}
	wait(script.Parent.Parent.Parent.Body.MP.Sound.TimeLength)
	if "rbxassetid://"..nowplaying == "rbxassetid://"..thisfunctionssong then
		makesong()
	end
end

function countVotes()
	local c = 0
	for _,v in pairs(votes) do
		if v[2] == true then
			c = c +1
		end
	end
	local remainder = #game.Players:GetPlayers() - #votes
	local percent = (c + (remainder/2))/#game.Players:GetPlayers()
	script.Parent.musicEvents.voteChange:FireAllClients(percent)
	if percent <= 0.2 then
		--skip song
		makesong()
	end
	return
end

script.Parent.musicEvents.voteYes.OnServerEvent:connect(function(p)
	for _,v in pairs(votes) do
		if v[1] == p.Name then
			v[2] = true
			countVotes()
			return
		end
	end
	table.insert(votes, {p.Name, true})
	countVotes()
end)

script.Parent.musicEvents.voteNo.OnServerEvent:connect(function(p)
	for _,v in pairs(votes) do
		if v[1] == p.Name then
			v[2] = false
			countVotes()
			return
		end
	end
	table.insert(votes, {p.Name, false})
	countVotes()
end)
