-- Get all possible gamepasses

for i,v in pairs(Tycoon:WaitForChild('Buttons'):GetChildren()) do
	if v:FindFirstChild('Gamepass') then
		if v.Gamepass.Value > 0 then
			GamePasses[v.Gamepass.Value] = v -- the button
		end
	end
end

MarketplaceService.ProcessReceipt = function(receiptInfo)
	for i,plr in pairs(game.Players:GetPlayers()) do
		if plr.userId == receiptInfo.PlayerId then
			if DevProducts[receiptInfo.ProductId] then
				for p,b in pairs(script.Parent.Tycoons[plr.team.name]:WaitForChild('Buttons'):GetChildren()) do
					if b:FindFirstChild('DevProduct') then
						if b.DevProduct.Value == receiptInfo.ProductId then
							local PlrT = game.ServerStorage.PlayerMoney:WaitForChild(plr.Name).OwnsTycoon
							if PlrT.Value ~= nil then
								local PlayerStats = game.ServerStorage.PlayerMoney:FindFirstChild(plr.Name)
								Create({[1] = 0,[2] = b,[3] = PlayerStats}, PlrT.Value.BuyObject)
							end
                            break
						end
					end
				end
			end
		end
	end
end

local function gamepassPurchaseFinished(player,gamepass,bought)
	if bought then
		for i,plr in pairs(game.Players:GetPlayers()) do
			if plr.userId == player.userId then
				if GamePasses[gamepass] then
					for p,b in pairs(script.Parent.Tycoons[plr.team.name]:WaitForChild('Buttons'):GetChildren()) do
						if b:FindFirstChild('Gamepass') then
							if b.Gamepass.Value == gamepass then
								local PlrT = game.ServerStorage.PlayerMoney:WaitForChild(plr.Name).OwnsTycoon
								if PlrT.Value ~= nil then
									local PlayerStats = game.ServerStorage.PlayerMoney:FindFirstChild(plr.Name)
									Create({[1] = 0,[2] = b, [3] = PlayerStats}, PlrT.Value.BuyObject)
								end
                            	break
							end
						end
					end
				end
			end
		end
	end
end

MarketplaceService.PromptGamePassPurchaseFinished:Connect(gamepassPurchaseFinished)

function Create(tab, prnt)
	local x = Instance.new('Model')
	Instance.new('NumberValue',x).Value = tab[1]
	x.Value.Name = "Cost"
	Instance.new('ObjectValue',x).Value = tab[2]
	x.Value.Name = "Button"
	local Obj = Instance.new('ObjectValue',x)
	Obj.Name = "Stats"
	Obj.Value = tab[3]
	x.Parent = prnt
end
